flatMap<R> method

Iterable<R> flatMap<R>(
  1. Iterable<R> transform(
    1. T element
    )
)

Returns a new lazy Iterable containing elements yielded from the results of the transform function being invoked on each element of this collection.

This method applies the transform function to each element of the current iterable and yields all the elements from the resulting iterables. The resulting iterable is a flat collection of all the elements produced by the transform function.

  • Parameters:

    • transform: A function that takes an element of the collection and returns an iterable of elements of type R.
  • Returns: A new lazy Iterable with elements yielded from the transform function.

  • Examples:

    Iterable<String> words = ['hello', 'world'];
    Iterable<String> letters = words.flatMap((word) => word.split(''));
    print(letters); // ['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']
    

Note: This method does not modify the original iterable. It creates a new lazy iterable with the flattened elements produced by the transform function, leaving the original iterable unchanged.

Implementation

Iterable<R> flatMap<R>(Iterable<R> transform(T element)) sync* {
  for (var current in this) {
    yield* transform(current);
  }
}