var slice = Array.prototype.slice,
call = Function.prototype.call;
function toArray() {
return call.apply(slice, arguments);
}
var spicyCurry = function(fn) {
var filled = toArray(arguments, 1); // like doing arguments.slice(1)
return makeCurry(fn, filled);
}
var makeCurry = function(fn, filled) {
return function() {
var args = toArray(arguments);
var nfilled = filled.concat(args);
var retFn = makeCurry(fn, nfilled);
if (nfilled.length >= fn.length) {
var value = fn.apply(this, nfilled.slice(-fn.length));
retFn.valueOf = function(){ return value; }
}
return retFn;
}
}
// This function only takes three arguments so that the examples below
// better illustrates what the _curry_ function is doing.
var add = spicyCurry(function(a,b,c) {
return a + b + c;
});
// These should each print out 60.
console.log(add(10, 20, 30));
console.log(add(10, 20)(30));
console.log(add(10)(20, 30));
console.log(add(10)(20)(30));
// These should each print out 90.
console.log(add(10, 20, 30, 40));
console.log(add(10, 20)(30, 40));
console.log(add(10)(20, 30, 40));
console.log(add(10)(20)(30)(40));
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.