Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 6x 6x 8x 12x 24x | import { Mapper } from "@benneq/function";
/**
* Maps an {@link Iterable} to a flat representation of the mapped
* {@link Iterable}s.
*
* @example
* ```ts
* const mapTo = flatMap(x => [1, x]);
* const result = mapTo([1, 2, 3]);
* console.log(result); // [1,1,1,2,1,3]
* ```
*
* @see {@link chunk}, {@link groupBy}, {@link map} and {@link zip} for
* similar operations.
*
* @typeParam T - the source {@link Iterable} value type
* @typeParam R - the target {@link Iterable} value type
* @param mapper
* @returns an {@link Iterable} that emits all elements of all mapped {@link Iterable}s
*/
export const flatMap = <T, R>(mapper: Mapper<T, Iterable<R>>) =>
function* (iterable: Iterable<T>): IterableIterator<R> {
for (const value of iterable) {
for (const innerValue of mapper(value)) {
yield innerValue;
}
}
};
|