Last active
December 6, 2018 07:44
-
-
Save indongyoo/da8da7eddd4533d501d552865c773224 to your computer and use it in GitHub Desktop.
reduce+generator
This file contains hidden or 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 reduce(f, acc, iter) { | |
if (arguments.length == 2) { | |
iter = acc[Symbol.iterator](); | |
acc = iter.next().value; | |
} | |
for (const a of iter) acc = f(acc, a); | |
return acc; | |
} | |
reduce((a, b) => a + b, function*() { | |
yield 1; | |
yield 2; | |
yield 3; | |
} ()); | |
// 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment