Currying Functions in Scala with Examples - GeeksforGeeks

13 Feb, 2019
Currying in Scala is simply a technique or a process of transforming a function. This function takes multiple arguments into a function that takes single argument. It is applied widely in multiple functional languages. Syntax
def function name(argument1, argument2) = operation
Let’s understand with a simple example,Example:
// Scala program add two numbers// using currying Functionobject Curry{    // Define currying function    def add(x: Int, y: Int) = x + y;      def main(args: Array[String])    {        println(add(20, 19));    }}
Output:
39
Here, we have define add function which takes two arguments (x and y) and the function simply adds x and y and gives us the result, calling it in the main function.
Another way to declare currying functionSuppose, we have to transform this add function into a Curried function, that is transforming the function that takes two(multiple) arguments into a function that takes one(single) argument. Syntax
def function name(argument1) = (argument2) => operation
Example
// Scala program add two numbers// using Currying function  object Curry{    // transforming the function that     // takes two(multiple) arguments into     // a function that takes one(single) argument.    def add2(a: Int) = (b: Int) => a + b;      // Main method    def main(args: Array[String])    {        println(add2(20)(19));    }}
Output:
39
Here, we have define add2 function which takes only one argument a and we are going to return a second function which will have the value of add2. The second function will also take an argument let say b and this function when called in main, takes two parenthesis(add2()()), where the first parenthesis is of the function add2 and second parenthesis is of the second function. It will return the addition of two numbers, that is a+b. Therefore, we have curried the add function, which means we have transformed the function that takes two arguments into a function that takes one argument and the function itself returns the result.
Example
// Scala program add two numbers// using Currying functionobject Curry{    def add2(a: Int) = (b: Int) => a + b;      // Main function    def main(args: Array[String])    {        // Partially Applied function.        val sum = add2(29);        println(sum(5));    }}
Output:
34
Here, only one argument is passed while assigning the function to the value. The second argument is passed with the value and these arguments are added and result is printed.
Also, another way(syntax) to write the currying function. Syntax
def function name(argument1) (argument2) = operation
Example
// Scala program add two numbers// using Currying functionobject Curry{    // Curring function declaration    def add2(a: Int) (b: Int) = a + b;      def main(args: Array[String])    {        println(add2(29)(5));    }}
Output:
34
For this syntax, the Partial Application function also changes.Example
// Scala program add two numbers// using Currying functionobject Curry{    // Curring function declaration    def add2(a: Int) (b: Int) = a + b;      def main(args: Array[String])    {       // Partially Applied function.        val sum=add2(29)_;        println(sum(5));    }}
Output:
34
Here, only the ‘_’ is added after the calling the function add2 for value of sum.
Scala
Scala-Method
Scala
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Scala List filter() method with example
Scala Map
Type Casting in Scala
Scala List contains() method with example
Scala Lists
Scala Tutorial – Learn Scala with Step By Step Guide
Operators in Scala
Class and Object in Scala
Inheritance in Scala
Scala | Arrays