Created
October 10, 2012 16:35
-
-
Save hughfdjackson/3866752 to your computer and use it in GitHub Desktop.
reducify
This file contains hidden or 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
/* | |
turns a binary function into a variadic function via reduction | |
*/ | |
var reducify = function(fn){ | |
return function(){ | |
var args = [].slice.call(arguments) | |
return args.reduce(fn) | |
} | |
} | |
/* playing around */ | |
var before = function(str, splitBy){ return str.split(splitBy)[0] }, | |
beforeAll = reducify(before) | |
before('abc', 'c') // 'ab' | |
beforeAll('abc', 'c', 'b') // 'a' | |
var add = function(a, b){ return a + b }, | |
sum = reducify(add) | |
add(1, 2) // 3 | |
sum(1, 2, 3, 4) // 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment