O'Reilly logo

Pragmatic Scala by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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(_, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required