Skip to content

Instantly share code, notes, and snippets.

@mattfield
Forked from hughfdjackson/reducify.js
Created September 16, 2013 09:28
Show Gist options
  • Save mattfield/6578446 to your computer and use it in GitHub Desktop.
Save mattfield/6578446 to your computer and use it in GitHub Desktop.
/*
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