Bernhard
05/02/2019, 12:49 PMthanksforallthefish
05/02/2019, 1:05 PMBernhard
05/02/2019, 1:12 PMhho
05/02/2019, 1:17 PMBernhard
05/02/2019, 1:22 PMfun <T, V> adjacentGroupBy(values: Iterable<T>, criterium: (T) -> V): List<List<T>> {
val result = ArrayList<ArrayList<T>>()
for (value in values) {
if (result.isEmpty()) {
result.add(arrayListOf(value))
} else {
if (criterium(result.last().last()) == criterium(value)) {
result.last().add(value)
} else {
result.add(arrayListOf(value))
}
}
}
return result
}
thanksforallthefish
05/02/2019, 1:24 PMwindowed
works, you can set a window size of 2 and a step of 1, so each element is processed along with the next oneBernhard
05/02/2019, 1:33 PMhho
05/02/2019, 1:33 PMgroupBy
always means "assign to some common key".Bernhard
05/02/2019, 1:34 PMAlowaniak
05/02/2019, 2:14 PMfun <T, U> List<T>.adjacentGroupBy(selector: (T)->U) : List<List<T>> {
return fold(mutableListOf<MutableList<T>>()) { acc, e ->
acc.apply {
when {
isEmpty() -> add(mutableListOf(e))
selector(e) == selector(last().last()) -> last().add(e)
else -> add(mutableListOf(e))
}
}
}
}
karelpeeters
05/02/2019, 5:56 PMBernhard
05/04/2019, 9:49 AM