guipn/sinful.js

does reducify have a place in sinful.js?

Closed this issue · 1 comments

From https://gist.github.com/3866752

/* 
     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

Absolutely. I suggest writing it using the liberated version of Array.prototype.slice ;-)

Let me know if you want me to add it -- else I'll wait for your pull request.

Nice :-)

p.s.: you should pull, by the way. Your name's on the readme file now.