Currying is a concept rooted in mathematics and has also found it is way in computer science. Currying is a process of translating a function taking multiple arguments to a set of functions accepting only one argument. It is named after Haskell Curry! Currying in computer science is also done in a similar manner. You can read more on the background here.
h(x)(y)(z) = f(x,y,z)
h = curry(f)
Let’s see how we can apply currying in Scala. Well, it’s not difficult to curry a function. See Below
If you just pass addCurried(10) you would get an error. Which is not good and to be fair the function did require all the parameters to evaluate. Getting around this is simple with partially applied functions, these should not be confused with partial functions. See Below
Another example of a partially applied function
How about currying an existing function. That is also possible via Function.curried. It converts a function of N parameters to a curried function of order N. See Below
Currying is a powerful feature in Functional Programming languages like Haskell. Though its usage may be questioned in Scala due to the hybrid nature of the language. However, it can prove to be a valuable tool, when applying them to collections using transforming functions like map, reduce, fold etc..
Till next time!