July 2017
Intermediate to advanced
284 pages
6h 45m
English
It’s time to meet multiple parameter lists. We’re mostly used to single parameter lists with multiple parameters. However, in Scala you can also have multiple parameter lists, each with multiple parameters.
| | def totalPrices( |
| | prices : List[Int])(selector : Int => Boolean) = { |
| | prices.foldLeft(0) { (total, price) => |
| | if (selector(price)) total + price else total |
| | } |
| | } |
In this version of the totalPrices function, rather than accepting two parameters in one parameter list, you have two parameter lists, each with one parameter. This allows you to invoke the method using the following syntax:
| | totalPrices(prices) { price => price > 40 } |
You’re attaching the function call to the end of the method ...
Read now
Unlock full access