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

Get Pragmatic Scala 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.