flatMap
flatMap() function
Returns flattened Iterable/AsyncIterable of values by running each element flattening the mapped results.
Signature:
declare function flatMap<A, B = unknown>(f: (a: A) => B, iterable: Iterable<A>): IterableIterator<DeepFlatSync<B, 1>>;
Returns:
IterableIterator<DeepFlatSync<B, 1>>
Example
const iter = flatMap(s => s.split(" "), ["It is", "a good", "day"]);
iter.next() // {done:false, value: "It"}
iter.next() // {done:false, value: "is"}
iter.next() // {done:false, value: "a"}
iter.next() // {done:false, value: "good"},
iter.next() // {done:false, value: "day"},
iter.next() // {done:true, value: undefined}
// with pipe
pipe(
["It is", "a good", "day"],
flatMap(s => s.split(" ")),
toArray,
); // ["It", "is", "a", "good", "day"]
await pipe(
Promise.resolve(["It is", "a good", "day"]),
flatMap(s => s.split(" ")),
toArray,
); // ["It", "is", "a", "good", "day"]
// if you want to use asynchronous callback
await pipe(
Promise.resolve(["It is", "a good", "day"]),
toAsync,
flatMap(async (s) => s.split(" ")),
toArray,
); // ["It", "is", "a", "good", "day"]