deepFlatten<E> method

Iterable<E> deepFlatten<E>()

Flattens arbitrarily nested Iterables with elements of type E. Throws an ArgumentError when encountering an value of an unexpected type.

For example:

final input = [1, 2, [3, 4, [5, 6]]];
print(input.deepFlatten());   // [1, 2, 3, 4, 5, 6]

Implementation

Iterable<E> deepFlatten<E>() sync* {
  for (final value in this) {
    if (value is E) {
      yield value;
    } else if (value is Iterable) {
      yield* value.deepFlatten<E>();
    } else {
      throw ArgumentError.value(value, 'value', 'Invalid value');
    }
  }
}