I need to implement a function in Kotlin:
fun <T> partitionConsecutiveWithPredicate(input : List<T>, pred : (T, T) -> Boolean) : List<List<T>>
The function partitions the input list into a list of lists when every sub-list in the resulting list is a group of consecutive elements from the original list which conform to the provided predicate.
Example #1 - invoking the method on the Int list of
[2, 2, 2, 2, 5, 5, 8, 2, 2, 3]
with the predicate { t1 : Int, t2 : Int -> t1 == t2}
should result in [[2, 2, 2, 2], [5, 5], [8], [2, 2], [3]]
Example #2 - invoking the method on the Int list of
[1, 2, 3, 4, 3, 2, 1, 2]
with the predicate { t1 : Int, t2 : Int -> t2 == t1 + 1}
should result in [[1, 2, 3, 4], [3], [2], [1, 2]]
Naturally, I can achieve that with a traditional while-loop and list manipulation library functions, but I want to do it in a purely functional style, i.e. only using lambdas/sequences (no explicit for/while loops allowed). I tried using runningReduce and runningFold, but it creates a resulting list in every step, which isn't desirable in this case (you should create a sub-list only when the predicate stops being satisfied).
EDIT: It's been brought to my attention that a similar question exists, but said question doesn't emphasize the need for a functional solution, therefore I'm leaving this one as is.