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 | import { every } from './iter';
|
11 |
|
12 | /**
|
13 | * Iterate several iterables in lockstep.
|
14 | *
|
15 | * @param objects - The iterable objects of interest.
|
16 | *
|
17 | * @returns An iterator which yields successive tuples of values where
|
18 | * each value is taken in turn from the provided iterables. It will
|
19 | * be as long as the shortest provided iterable.
|
20 | *
|
21 | * #### Example
|
22 | * ```typescript
|
23 | * import { zip } from '@lumino/algorithm';
|
24 | *
|
25 | * let data1 = [1, 2, 3];
|
26 | * let data2 = [4, 5, 6];
|
27 | *
|
28 | * let stream = zip(data1, data2);
|
29 | *
|
30 | * Array.from(stream); // [[1, 4], [2, 5], [3, 6]]
|
31 | * ```
|
32 | */
|
33 | export function* zip<T>(...objects: Iterable<T>[]): IterableIterator<T[]> {
|
34 | const iters = objects.map(obj => obj[Symbol.iterator]());
|
35 | let tuple = iters.map(it => it.next());
|
36 | for (; every(tuple, item => !item.done); tuple = iters.map(it => it.next())) {
|
37 | yield tuple.map(item => item.value);
|
38 | }
|
39 | }
|