flatMap() function
각 요소를 실행하여 매핑된 결과를 평탄화한 값들의 평탄화된 Iterable/AsyncIterable을 반환합니다.
Signature:
typescript
declare function flatMap<A, B = unknown>(
f: (a: A) => B,
iterable: Iterable<A>,
): IterableIterator<DeepFlatSync<B, 1>>;Example
ts
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"]