chunkedBy

fun <T> List<T>.chunkedBy(predicate: (T) -> Boolean): List<List<T>>

Splits this collection into a list of lists, where each inner list is a chunk of elements separated by an element that matches the given predicate. The separator element itself is not included in the resulting chunks.

For example, [1, 2, 0, 3, 4, 0, 5].chunkedBy { it == 0 } would result in [[1, 2], [3, 4], [5]].

An empty list is returned if the original list is empty. If no elements match the predicate, a single list containing all elements of the original list is returned.

Return

A list of lists, where each list contains the elements between separators.

Parameters

predicate

A function that returns true for elements that should be treated as separators.