natario1
07/17/2020, 2:19 PMgroupBy { }, but only group on elements where the key is contiguous?
Say I have a List<Sample> where sample is class Sample(duration: Int).
I need something like a List<List<Sample>>, where samples are grouped by duration, but not reordered. If durations are 100, 100, 100, 200 and 100, that should be three groups, not two.pedro
07/17/2020, 2:22 PMfold. Not the simplest use of it, but doable…
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/fold.htmlnatario1
07/17/2020, 2:24 PMsamples.fold(mutableListOf<MutableList<Sample>>()) { groups, sample ->
val duration = groups.lastOrNull()?.lastOrNull()?.duration
if (sample.duration == duration) {
groups.last().add(sample)
} else {
groups.add(mutableListOf(sample))
}
groups
}pedro
07/17/2020, 2:26 PMdiesieben07
07/17/2020, 2:31 PMKroppeb
07/17/2020, 3:02 PM