Skip to content

Instantly share code, notes, and snippets.

@indongyoo
Last active December 6, 2018 07:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indongyoo/da8da7eddd4533d501d552865c773224 to your computer and use it in GitHub Desktop.
Save indongyoo/da8da7eddd4533d501d552865c773224 to your computer and use it in GitHub Desktop.
reduce+generator
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