Mohammed Oucif
05/24/2024, 9:25 AMSam
05/24/2024, 9:29 AMKlitos Kyriacou
05/24/2024, 9:36 AMval
doesn't "prevent the changing of the list instance". The instance itself can be changed (modified). It's the variable that points to the instance that can't be changed to point to another instance.Mohammed Oucif
05/24/2024, 9:53 AMStephan Schröder
05/24/2024, 1:12 PMephemient
05/24/2024, 4:05 PMfun <E> Iterable<E>.groupConsecutiveBy(predicate: (E, E) -> Boolean): List<List<E>> = buildList {
var group = mutableListOf<E>()
for (item in this@groupConsecutiveBy) {
if (group.isEmpty() || predicate(group.last(), item)) {
group.add(item)
} else {
if (group.isNotEmpty()) add(group)
group = mutableListOf(item)
}
}
if (group.isNotEmpty()) add(group)
}
ephemient
05/24/2024, 4:06 PMfold
but either having a mutable accumulator is the same sort of weirdness - usually you don't want it, but sometimes it's the right solution