-
Notifications
You must be signed in to change notification settings - Fork 3
/
_zip.js
36 lines (30 loc) · 788 Bytes
/
_zip.js
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
31
32
33
34
35
36
import {list, map, iter, _next} from '../index.js';
/**
* Zips iterables together. Yields a tuple containing the first element of each
* iterable, then a tupe containing the second element of each iterable, etc.
* Stops when one of the iterables runs out of elements.
*
* @example
* // returns [ [ 'a' , 1 ] , [ 'b' , 2 ] , [ 'c' , 3 ] ]
* list( _zip( [ 'abcd' , range(3) ] ) ) ;
*
* @param {Iterable[]} iterables - The iterables to zip.
* @returns {Iterator}
*
*/
export function* _zip(iterables) {
const iterators = list(map(iter, iterables));
if (iterators.length === 0) {
return;
}
while (true) {
const buffer = [];
for (const result of map(_next, iterators)) {
if (result.done) {
return;
}
buffer.push(result.value);
}
yield buffer;
}
}