Java Reference
In-Depth Information
Currying
Currying is a process that involves the partial application of functions. It's amed after the
logician Haskell Curry —not the spicy food—just like the programming language Haskell is.
His work on a paper by Moses Schönfinkel lead to the development of this programming
technique.
A function is said to be curried when not all arguments have been supplied to the function,
so it returns another function that retains the arguments given and expects the remaining ar-
guments. Here is a multiplier function that expects two arguments and multiplies those
numbers together. If only one argument is provided, it returns a function that expects only
one argument and will multiply it by the argument supplied to the first function:
function multiplier(x,y) {
if (y === undefined) {
return function(z) {
return x * z;
}
} else {
return x * y;
}
}
This function uses a closure to trap the first variable, x, inside a new function that's re-
turned. This new function can then be used to provide the second argument. If both argu-
ments are provided, the function simply multiplies them together:
multiplier(3,5);
<< 15
Let's have a look at what happens if only the first argument is passed to this function: