Multiple Parameter Lists

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 ...

Get Functional Programming: A PragPub Anthology now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.