Created
April 2, 2015 03:47
-
-
Save TechNinjaWeb/f5c7803a4a74a7546bc2 to your computer and use it in GitHub Desktop.
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
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