Last active
October 2, 2015 03:01
-
-
Save nickbalestra/e22e7993f25965486f67 to your computer and use it in GitHub Desktop.
Recursive reduce --es6 tail call optmized
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
// 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