Skip to content

Instantly share code, notes, and snippets.

@lac5
Created November 27, 2019 20:07
Show Gist options
  • Save lac5/94a0d77cebb4a733cc51ad4b4f4da987 to your computer and use it in GitHub Desktop.
Save lac5/94a0d77cebb4a733cc51ad4b4f4da987 to your computer and use it in GitHub Desktop.
function zipMin(...iterators) {
let done = iterators.length === 0;
iterators = iterators.map(iterator => iterator[Symbol.iterator]());
return {
[Symbol.iterator]() {
return this;
},
next(...args) {
let value = [];
for (let i = 0, length = iterators.length; i < length; i++) {
let output = iterators[i].next(...args);
value[i] = output.value;
done = done || output.done;
}
return { value, done };
},
throw(...args) {
let value = [];
for (let i = 0, length = iterators.length; i < length; i++) {
let output = iterators[i].throw(...args);
value[i] = output.value;
done = done || output.done;
}
return { value, done };
},
};
}
function zipMax(...iterators) {
let done = iterators.length === 0;
iterators = iterators.map(iterator => iterator[Symbol.iterator]());
return {
[Symbol.iterator]() {
return this;
},
next(...args) {
let value = [];
let allDone = !done;
for (let i = 0, length = iterators.length; i < length; i++) {
let output = iterators[i].next(...args);
value[i] = output.value;
allDone = allDone && output.done;
}
done = done || allDone;
return { value, done };
},
throw(...args) {
let value = [];
let allDone = !done;
for (let i = 0, length = iterators.length; i < length; i++) {
let output = iterators[i].next(...args);
value[i] = output.value;
allDone = allDone && output.done;
}
done = done || allDone;
return { value, done };
},
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment