UNPKG

1.11 kBPlain TextView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
3/*-----------------------------------------------------------------------------
4| Copyright (c) 2014-2017, PhosphorJS Contributors
5|
6| Distributed under the terms of the BSD 3-Clause License.
7|
8| The full license is in the file LICENSE, distributed with this software.
9|----------------------------------------------------------------------------*/
10/**
11 * Transform the values of an iterable with a mapping function.
12 *
13 * @param object - The iterable object of interest.
14 *
15 * @param fn - The mapping function to invoke for each value.
16 *
17 * @returns An iterator which yields the transformed values.
18 *
19 * #### Example
20 * ```typescript
21 * import { map } from '@lumino/algorithm';
22 *
23 * let data = [1, 2, 3];
24 *
25 * let stream = map(data, value => value * 2);
26 *
27 * Array.from(stream); // [2, 4, 6]
28 * ```
29 */
30export function* map<T, U>(
31 object: Iterable<T>,
32 fn: (value: T, index: number) => U
33): IterableIterator<U> {
34 let index = 0;
35 for (const value of object) {
36 yield fn(value, index++);
37 }
38}