Skip to content

Instantly share code, notes, and snippets.

@hughfdjackson
Created October 10, 2012 16:35
Show Gist options
  • Save hughfdjackson/3866752 to your computer and use it in GitHub Desktop.
Save hughfdjackson/3866752 to your computer and use it in GitHub Desktop.
reducify
/*
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