Java Reference
In-Depth Information
We can see that a new function is returned that keeps the value of x that was provided to
the multiplier() function:
quadrupler(5);
<< 20
Because the first call to a curried function returns another function, we can invoke the
whole function by passing each argument in separate pairs of parentheses:
multiplier(8)(11);
<< 88
This works because multiplier(8) returns a function, and we're immediately passing
the argument 11 to that function.
Curring allows you to turn a single function into a series of functions instead. This is useful
if you find that you're constantly calling a function with the same argument. For example,
if you were frequently using the same value in the multiplier() function to calculate
a tax rate of 22%:
tax = multiplier(0.22,400); // calculate tax on 400
<< 88
If you found yourself doing this often, it would make sense to create a new curried function
using 0.22 as the first argument:
calcTax = multiplier(0.22);
<< function (z){
return x * z;
}
calcTax(400);
<< 88
By currying the multiplier() function, we've created a new function, calcTax() ,
that is simpler to use.
Search WWH ::




Custom Search