split method

Iterable<Iterable<T>> split(
  1. T separator,
  2. {bool comparer(
    1. T separator,
    2. T element
    )?}
)

Splits the is iterable into multiple iterables on elements that match the separator.

Optionally a comparer can be provided to determine if an element is equal to the separator.

Example:

void main() {
  final list = ['a', ' ', 'b', 'c', ' ', 'd'];
  final result = list.split(' ');

  // Result: [
  //   ['a''],
  //   ['b', 'c'],
  //   ['d'],
  // ]
}

Implementation

Iterable<Iterable<T>> split(
  T separator, {
  bool Function(T separator, T element)? comparer,
}) sync* {
  var buffer = <T>[];

  comparer ??= EqualityComparer.forType<T>().compare;

  for (var o in this) {
    if (comparer(separator, o)) {
      yield List.unmodifiable(buffer);
      buffer = <T>[];
    } else {
      buffer.add(o);
    }
  }

  if (buffer.isNotEmpty) {
    yield List.unmodifiable(buffer);
  }
}