Currying allows you to easily create custom functions by partially invoking an existing function. Here’s a simple example:
var add = function(a,b) {
return a + b;
}
//create function that returns 10 + argument
var addTen = add.curry(10);
addTen(20); //30
// ----------------------------------------
Function.prototype.curry = function() {
if (arguments.length<1) {
return this; //nothing to curry with - return function
}
var fToBind = this;
var args = Array.prototype.slice.call(arguments); // Converting to array
return function() {
return fToBind.apply(this, args.concat(toArray(arguments)));
}
}
var add = function(a,b) {
return a + b;
}
//create function that returns 10 + argument
var addTen = add.curry(10);
addTen(20); //30
// ----------------------------------------
Function.prototype.curry = function() {
if (arguments.length<1) {
return this; //nothing to curry with - return function
}
var fToBind = this;
var args = Array.prototype.slice.call(arguments); // Converting to array
return function() {
return fToBind.apply(this, args.concat(toArray(arguments)));
}
}
No comments:
Post a Comment