do we have anything like this in the stdlib? Not s...
# stdlib
k
do we have anything like this in the stdlib? Not sure how useful it is, I just needed it for something I was doing though
Copy code
/**
 * Splits the receiver iterable into sublists, where a new sublist is started with every element
 * that returns true when [splitPredicate] is tested against it.
 */
fun <T> Iterable<T>.splitWhen(
        splitPredicate: (index: Int, element: T, currentSubList: List<T>) -> Boolean
): List<List<T>> {
    val result = mutableListOf<MutableList<T>>()
    forEachIndexed { index, element ->
        if (index == 0 || splitPredicate(index, element, result.last())) {
            result.add(mutableListOf<T>())
        }
        result.last().add(element)
    }
    return result
}