Skip to content

Instantly share code, notes, and snippets.

@nickbalestra
Last active October 2, 2015 03:01
Show Gist options
  • Save nickbalestra/e22e7993f25965486f67 to your computer and use it in GitHub Desktop.
Save nickbalestra/e22e7993f25965486f67 to your computer and use it in GitHub Desktop.
Recursive reduce --es6 tail call optmized
// Recursive implementation of reduce
// Supports ES6 interpreter Tail Call Optimization
// http://www.ecma-international.org/ecma-262/6.0/#sec-tail-position-calls
// https://kangax.github.io/compat-table/es6/#test-proper_tail_calls_(tail_call_optimisation)
function recursiveReduce(list, callback, accumulator) {
if (list.length === 0) return accumulator;
accumulator === undefined ?
accumulator = list[0] :
accumulator = callback(accumulator, list[0]);
return recursiveReduce(list.slice(1), callback, accumulator);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment