Curry Example

by ncapito

HTML

<div class='wrapper'>
    <div>Result: <span class='result'></span> 
    </div>
    <div>Curry Result: <span class='curryResult'></span> 
    </div>
    <button class='go'>Calculate</button>
</div>

JavaScript

Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
};

Function.method('curry', function () {
    var slice = Array.prototype.slice,
        args = slice.apply(arguments),
        that = this;
    console.log(args);
    return function () {
        console.log(arguments, args.concat(slice.apply(arguments)));
        return that.apply(null,
        args.concat(slice.apply(arguments)));
    };
});

var add = function (a, b) {
    return [].slice.apply(arguments).reduce(function (a, b) {
        return a + b;
    });
};
var add1 = add.curry(1);


$('.go').on('click', function (e) {
    var that = this,
        $result = $(that).closest('.wrapper').find('.result'),
        $curryResult = $(that).parent('.wrapper').find('.curryResult');

    $result.html(add(1, 2, 3, 4));
    $curryResult.html(add1(2, 3, 4));
});