Skip to content

map() function

各要素に f を適用して実行した値の Iterable/AsyncIterable を返します。

map が副作用を引き起こす場合は、代わりに mapEffect を使用することを推奨します。

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.