Group, split, and slide for different questions
groupBy creates lists under keys, partition divides elements into two groups, and windowed builds overlapping or stepped windows. Choose the operation matching your analysis rather than manually maintaining several mutable lists.
fun main() {
val durations = listOf(10, 20, 30, 40)
val (short, long) = durations.partition { it < 25 }
check(short == listOf(10, 20))
check(long == listOf(30, 40))
check(durations.windowed(2) == listOf(listOf(10, 20), listOf(20, 30), listOf(30, 40)))
println(durations.groupBy { it / 20 })
}
Window size, step, and partial-window policy determine which trailing elements appear. groupBy retains every element, so use groupingBy(...).eachCount() when only frequencies are needed.
Exercise
Compute rolling averages for three-day study totals. Define whether fewer than three days produces no result or a partial average, and implement that policy explicitly.
Check: test empty input, two days, three days, and four days. Verify the number of resulting windows before checking their averages.