splitList method

Iterable<List<T>> splitList(
  1. bool test(
    1. T item
    ), {
  2. bool emptyParts = false,
})

Split a list, according to a predicate, removing the list item that satisfies the predicate.

[1,2,3,4,5].splitList((v)=>v==2 || v==4) ➜ [[1], [3], [5]]

Implementation

Iterable<List<T>> splitList(
  bool Function(T item) test, {
  bool emptyParts = false,
}) sync* {
  if (isEmpty) return;
  int start = 0;
  for (int i = 0; i < length; i++) {
    T element = this[i];
    if (test(element)) {
      if (start < i || emptyParts) {
        yield sublist(start, i);
      }
      start = i + 1;
    }
  }
  if (start < length || emptyParts) {
    yield sublist(start);
  }
}