splitBeforeIndexed method
Splits the elements into chunks before some elements and indices.
Each element and index except the first is checked using test
for whether it should start a new chunk.
If so, the elements since the previous chunk-starting element
are emitted as a list.
Any remaining elements are emitted at the end.
Example:
var parts = [1, 0, 2, 1, 5, 7, 6, 8, 9]
.splitBeforeIndexed((i, v) => i < v);
print(parts); // ([1], [0, 2], [1, 5, 7], [6, 8, 9])
Implementation
Iterable<List<T>> splitBeforeIndexed(
bool Function(int index, T element) test) sync* {
var iterator = this.iterator;
if (!iterator.moveNext()) {
return;
}
var index = 1;
var chunk = [iterator.current];
while (iterator.moveNext()) {
var element = iterator.current;
if (test(index++, element)) {
yield chunk;
chunk = [];
}
chunk.add(element);
}
yield chunk;
}