raulraja
07/10/2017, 9:37 AMfun List<Int>.bufferUntilChanged(): List<List<Int>> =
this.foldIndexed(emptyList<List<Int>>(), { index, acc, n ->
if (index > 0) {
val previous: Int = this[index - 1]
if (previous == n && acc.isNotEmpty()) {
val currentBuffer = acc.last() + n
val prevBuffers = acc.dropLast(1)
prevBuffers + listOf(currentBuffer)
} else {
acc + listOf(listOf(n))
}
} else {
acc + listOf(listOf(n))
}
})