O'Reilly logo

Functional Programming: A PragPub Anthology by Michael Swaine

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

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

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