Parameter Routing

Where it makes sense, you can make function values more concise than you’ve seen so far. Let’s first create an example to find the maximum of an array of values, using the Math.max method to compare two values:

 
val​ largest =
 
(​Integer​.MIN_VALUE /: arr) { (carry, elem) => Math.max(carry, elem)}

In the function value, we’re sending the parameters carry and elem as arguments to the method max to determine which of those two is larger. We use the result of that computation to eventually determine the largest element in the array. As you learned in the previous section, we can use _ to make the function value concise and eliminate the explicit parameters, like so:

 
val​ largest = (​Integer​.MIN_VALUE /: arr) { Math.max(_, _)}

The _ can represent not only a single parameter; it can represent the entire parameter list as well. We can modify the call to max as follows:

 
val​ largest = (​Integer​.MIN_VALUE /: arr) { Math.max _ }

In the previous code, the _ represents the entire parameter list, that is, (parameter1, parameter2). To merely pass the received parameters to the underlying method in the same order as received, we don’t even need the ceremony of the _. We can further simplify the previous code:

 
val​ largest = (​Integer​.MIN_VALUE /: arr) { Math.max }

The Scala compiler is doing quite a bit of work to verify that this code is syntactically correct. It first looks up the signature of the method /: to determine that it takes two parameter lists—the first parameter list expects an object and the second expects a function value. The compiler further determines that the function value should take two parameters. Once the compiler knows the signature of the expected function value, it decides the function value we provided takes two parameters since it’s missing the parameter list—there’s no => symbol; we only provided an implementation. The compiler also knows that the method max takes two arguments but we specified none. It puts two and two together and performs the direct routing.

During the compilation check, if the inference in any of these steps fail, the compiler will report an error. For instance, suppose in the function value we called a method that took more than two parameters but we didn’t specify any of the arguments. In this case the compiler will complain that it does not have enough arguments on hand, from the implicit parameters, to pass to that method.

Adjust the conciseness dial of Scala to the extent you’re comfortable. While making use of the conciseness, ensure that the code doesn’t become cryptic—strike that gentle balance.

You learned the different options to define function values. Function values are concise, but repeating the same function value in different calls will result in duplication. Let’s look at ways to remove that duplication.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.71.146