Created
November 27, 2019 20:07
-
-
Save lac5/94a0d77cebb4a733cc51ad4b4f4da987 to your computer and use it in GitHub Desktop.
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
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