Skip to content

Instantly share code, notes, and snippets.

@TechNinjaWeb
Created April 2, 2015 03:47
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 TechNinjaWeb/f5c7803a4a74a7546bc2 to your computer and use it in GitHub Desktop.
Save TechNinjaWeb/f5c7803a4a74a7546bc2 to your computer and use it in GitHub Desktop.
function reduce(arr, fn, initial) {
return (function reduceOne(index, value) {
if (index > arr.length - 1) return value // end condition
return reduceOne(index + 1, fn(value, arr[index], index, arr)) // calculate & pass values to next step
})(0, initial) // IIFE. kick off recursion with initial values
}
module.exports = reduce
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment