splitBetweenIndexed method Null safety

Iterable<List<T>> splitBetweenIndexed(
  1. bool test(
    1. int index,
    2. T first,
    3. T second
    )
)

Splits the elements into chunks between some elements and indices.

Each pair of adjacent elements and the index of the latter are checked using test for whether a chunk should end between them. If so, the elements since the previous chunk-splitting elements are emitted as a list. Any final elements are emitted at the end.

Example:

var parts = [1, 0, 2, 1, 5, 7, 6, 8, 9]
   .splitBetweenIndexed((i, v1, v2) => v1 > v2);
print(parts); // ([1], [0, 2], [1, 5, 7], [6, 8, 9])

Implementation

Iterable<List<T>> splitBetweenIndexed(
    bool Function(int index, T first, T second) test) sync* {
  var iterator = this.iterator;
  if (!iterator.moveNext()) return;
  var previous = iterator.current;
  var chunk = <T>[previous];
  var index = 1;
  while (iterator.moveNext()) {
    var element = iterator.current;
    if (test(index++, previous, element)) {
      yield chunk;
      chunk = [];
    }
    chunk.add(element);
    previous = element;
  }
  yield chunk;
}