before method

  1. @lazy
  2. @useResult
Iterable<List<E>> before(
  1. Predicate<E> predicate
)

Splits this iterable's elements into lists before those that satisfy predicate.

See after for splitting elements after those that satisfy predicate.

final parts = [1, 2, 3, 4, 5, 6, 7, 8, 9].split.before(primeNumber);

print(parts); // [[1], [2], [3, 4], [5, 6], [7, 8, 9]]
//                 ^----^----^-------^-------^---------- prime numbers

Implementation

@lazy @useResult Iterable<List<E>> before(Predicate<E> predicate) sync* {
  final iterator = _iterable.iterator;
  if (!iterator.moveNext()) {
    return;
  }

  var chunk = [iterator.current];
  while (iterator.moveNext()) {
    final element = iterator.current;
    if (predicate(element)) {
      yield chunk;
      chunk = [];
    }

    chunk.add(element);
  }

  yield chunk;
}