Java Reference
In-Depth Information
But in Java the following will result in a compiler error because count is implicitly forced to be
final:
We argued in chapters 7 , 13 , and 14 that you should avoid mutation when possible to make your
programs easier to maintain and parallelizable, so use this feature only when strictly necessary.
15.2.3. Currying
In chapter 14 we described a technique called currying : where a function f of two arguments (x
and y, say) is seen instead as a function g of one argument, which returns a function also of one
argument. This definition can be generalized to functions with multiple arguments, producing
multiple functions of one argument. In other words, you can break down a function that takes
multiple arguments into a series of functions that take a subpart of the arguments. Scala
provides a construct to let you easily curry an existing function.
To understand what Scala brings to the table, let's first revisit an example in Java. You can
define a simple method to multiply two integers:
static int multiply(int x, int y) {
return x * y;
}
int r = multiply(2, 10);
But this definition requires all the arguments to be passed to it. You can manually break down
the multiply method by making it return another function:
 
Search WWH ::




Custom Search