I have a two ranges that represents a timeline of ...
# announcements
k
I have a two ranges that represents a timeline of some media (whole media is 0..30000, but I have a ranges like 0..15000 and 15001..30000). I also have a list of ranges how long user listened to some part of the media. Is there any function in stdlib that lets me easily calculate how long user spent in which range? Media ranges (0..30_000): 0..15_000 and 15_001..30_000 Listened ranges: (0..10_000), (5_000..15_000), (20_000..25_000) Result would be: For (0..15_000) range = 20_000 For (15_001..30_000) range = 5_000
This is what I came up with
Copy code
private fun LongRange.countIfPresentInRanges(ranges: List<LongRange>): Long {
    var sum = 0L
    forEach { value ->
        ranges.forEach {
            if (it.contains(value)) {
                sum++
            }
        }
    }
    return sum
}