Skip to content

Instantly share code, notes, and snippets.

@friendlyanon
Last active February 23, 2021 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save friendlyanon/959477b59187e8adfb80bddf598178a6 to your computer and use it in GitHub Desktop.
Save friendlyanon/959477b59187e8adfb80bddf598178a6 to your computer and use it in GitHub Desktop.
export function* scan(iterable, reducer, initialValue) {
const it = iterable[Symbol.iterator]();
let accumulator = initialValue;
let index = 0;
if (arguments.length < 3) {
const step = it.next();
if (step.done) {
return;
}
accumulator = step.value;
++index;
}
for (const value of it) {
accumulator = reducer(accumulator, value, index++);
yield accumulator;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment