Skip to content

Instantly share code, notes, and snippets.

@hraban hraban/interleave.js
Last active Jun 10, 2019

Embed
What would you like to do?
Interleaving two arrays in Javascript (using iterators & generators)
/**
* interleave([1,2], [8,7,6,5], [], 'abc')
* => [ 1, 8, 'a', 2, 7, 'b', 6, 'c', 5 ]
*/
function* interleave() {
const its = Array.from(arguments).map(x => x[Symbol.iterator]());
let done;
do {
done = true;
for (const it of its) {
const next = it.next();
if (!next.done) {
yield next.value;
done = false;
}
}
} while (!done)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.