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

Currying

Currying in Scala transforms a function that takes more than one parameter into a function that takes multiple parameter lists. If you’re calling a function multiple times with the same set of arguments, you can reduce the noise and spice up the code by using currying.

Let’s see how Scala provides support for currying. Instead of writing a method that takes one parameter list with multiple parameters, write it with multiple parameter lists with one parameter each; you may have more than one parameter in each list as well. That is, instead of def foo(a: Int, b: Int, c: Int) = {}, write it as def foo(a: Int)(b: Int)(c: Int) {}. You can then call it as, for example, foo(1)(2)(3), foo(1){2}{3}, or even foo{1}{2}{3}.

Let’s examine what goes ...

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