Skip to content

Instantly share code, notes, and snippets.

@bellbind
Last active April 26, 2022 23:06
Show Gist options
  • Save bellbind/353819de43bf694fba3b to your computer and use it in GitHub Desktop.
Save bellbind/353819de43bf694fba3b to your computer and use it in GitHub Desktop.
[es6] zip generator for ES6
"use strict";
const zip = function* () {
const its = Array.from(arguments, e => e[Symbol.iterator]());
while (true) {
const es = its.map(it => it.next());
if (es.some(e => e.done)) return;
yield es.map(e => e.value);
}
};
// example
for (const [a, b] of zip([1,2,3], ["a", "b", "c", "d"])) {
console.log(a, b);
}
for (const [a, b] of zip(Array.from(Array(12), (e, i) => i), function* () {
for (let i = 0; true; i++) {
yield String.fromCodePoint(0x1F550 + i);
}
}())) {
console.log(a, b);
}
@bellbind
Copy link
Author

bellbind commented Feb 9, 2016

NOTE: On nodejs-5, run with node --harmony_destructuring zip.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment