Skip to content

map() function

Returns Iterable/AsyncIterable of values by running each applying f.

If the map causes side effects, it is recommended to use mapEffect instead.

Signature:

typescript
declare function map<A, B>(f: (a: A) => B, iterable: Iterable<A>): IterableIterator<B>;

Example

ts
const iter = map(a => a + 10, [1, 2, 3, 4]);
iter.next() // {done:false, value: 11}
iter.next() // {done:false, value: 12}
iter.next() // {done:false, value: 13}
iter.next() // {done:false, value: 14},
iter.next() // {done:true, value: undefined}

// with pipe
pipe(
 [1, 2, 3, 4],
 map(a => a + 10),
 toArray,
); // [11, 12, 13, 14]

await pipe(
 Promise.resolve([1, 2, 3, 4]),
 map(a => a + 10),
 toArray,
); // [11, 12, 13, 14]

// if you want to use asynchronous callback
await pipe(
 Promise.resolve([1, 2, 3, 4]),
 toAsync,
 map(async (a) => a + 10),
 toArray,
); // [11, 12, 13, 14]

// with toAsync
await pipe(
 [Promise.resolve(1), Promise.resolve(2), Promise.resolve(3), Promise.resolve(4)],
 toAsync,
 map(a => a + 10),
 toArray,
); // [11, 12, 13, 14]

Try It

see pipe, toAsync, toArray

Open Source Code

Released under the Apache-2.0 License.