flatMap<A, B> function

ImmutableList<B> Function(ImmutableList<A>) flatMap<A, B>(
  1. ImmutableList<B> f(
    1. A
    )
)

Returns a function that takes an ImmutableList<A> and maps each item in the list using the provided function f to produce a new ImmutableList<B>.

Unlike the regular map function, flatMap expects the mapping function f to return an ImmutableList<B> for each item in the original list, and it will flatten the resulting lists into a single ImmutableList<B>.

Usage:

var myList = ImmutableList([1, 2, 3]);
var flatMapToLists = flatMap<int, int>((item) => ImmutableList([item, item + 10]));
var newList = flatMapToLists(myList); // Returns ImmutableList([1, 11, 2, 12, 3, 13])
  • Parameter f: The mapping function that takes an item of type A and returns an ImmutableList<B>.
  • Returns: A function that processes an ImmutableList<A> and produces a new ImmutableList<B> with mapped and flattened items.

Implementation

ImmutableList<B> Function(ImmutableList<A>) flatMap<A, B>(
        ImmutableList<B> Function(A) f) =>
    (ImmutableList<A> list) =>
        ImmutableList(list._items.expand((item) => f(item)._items).toList());