expand<T> method Null safety

Iterable<T> expand<T>(
  1. Iterable<T> f(
    1. E element
    )
)
override

Expands each element of this Iterable into zero or more elements.

The resulting Iterable runs through the elements returned by toElements for each element of this, in iteration order.

The returned Iterable is lazy, and calls toElements for each element of this iterable every time the returned iterable is iterated.

Example:

var pairs = [[1, 2], [3, 4]];
var flattened = pairs.expand((pair) => pair).toList();
print(flattened); // => [1, 2, 3, 4];

var input = [1, 2, 3];
var duplicated = input.expand((i) => [i, i]).toList();
print(duplicated); // => [1, 1, 2, 2, 3, 3]

Equivalent to:

Iterable<T> expand<T>(Iterable<T> toElements(E e)) sync* {
  for (var value in this) {
    yield* toElements(value);
  }
}

Implementation

Iterable<T> expand<T>(Iterable<T> f(E element)) =>
    ExpandIterable<E, T>(this, f);