after method
Splits this iterable's elements into lists after those that satisfy predicate
.
See before for splitting elements before those that satisfy predicate
.
final parts = [1, 2, 3, 4, 5, 6, 7, 8, 9].split.after(primeNumber);
print(parts); // ([1], [2], [3], [4, 5], [6, 7], [8, 9])
// ^----^----^-------^-------^----------- prime numbers
Implementation
@lazy @useResult Iterable<List<E>> after(Predicate<E> predicate) sync* {
List<E>? chunk;
for (final element in _iterable) {
(chunk ??= []).add(element);
if (predicate(element)) {
yield chunk;
chunk = null;
}
}
if (chunk != null) {
yield chunk;
}
}