Last active
April 26, 2022 23:06
-
-
Save bellbind/353819de43bf694fba3b to your computer and use it in GitHub Desktop.
[es6] zip generator for ES6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NOTE: On nodejs-5, run with
node --harmony_destructuring zip.js