kevinmost
04/14/2016, 11:49 PM/**
* 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
}