The name currying is a reference to the mathematician and logician Haskell Curry. The process of currying consists of transforming a function that takes multiple arguments into a sequence of functions, each with a single argument.
Before we start currying functions, we need to understand the difference between a function and a function value.
You are already familiar with functions—they begin with the keyword def
, take one or several parameter lists between ()
symbols, optionally declare a return type after a :
sign, and have a defined body after the =
sign, as shown in the following example:
def multiply(x: Int, y: Int): Int = x * y // multiply: multiply[](val x: Int,val y: Int) => Int
A function value (also called a function literal) is similar to any other value, such as "hello": String
, 3: Int
, or true: Boolean
. As with other values, you can pass a function value as an argument to a function, or assign it to a variable using the val...