Java Reference
In-Depth Information
A General Curry Function
In the last example, we hard coded the multiplier() function so that it could be cur-
ried. It's possible to use a curry() function to take any function and allow it to be par-
tially applied. The curry function is the following:
function curry(func) {
var fixedArgs = [].slice.call(arguments,1);
return function() {
args = fixedArgs.concat([].slice.call(arguments))
return func.apply(null, args);
};
}
This can now be applied to a more standard divider() function that returns the result of
dividing its two arguments:
function divider(x,y) {
return x/y;
}
divider(10,5);
<< 2
The curry() function can be used to create a more specific function that finds the recip-
rocal of numbers:
reciprocal = curry(divider,1);
<< function () {
args = fixedArgs.concat([].slice.call(arguments))
return func.apply(null, args);
}
reciprocal(2);
<< 0.5
Search WWH ::




Custom Search