Currying Functions in Scala with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program add two numbers
// using currying Function
object Curry
{
    // Define currying function
    def add(x: Int, y: Int) = x + y;
  
    def main(args: Array[String])
    {
        println(add(20, 19));
    }
}

chevron_right


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 function
Suppose, 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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


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.

Currying Function Using Partial Application

We have another way to use this Curried function and that is Partially Applied function. So, let’s take a simple example and understand. we have defined a variable sum in the main function

Example

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program add two numbers
// using Currying function
object 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));
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program add two numbers
// using Currying function
object Curry
{
    // Curring function declaration
    def add2(a: Int) (b: Int) = a + b;
  
    def main(args: Array[String])
    {
        println(add2(29)(5));
    }
}

chevron_right


Output:

34

For this syntax, the Partial Application function also changes.
Example

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program add two numbers
// using Currying function
object 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));
    }
}

chevron_right


Output:

34

Here, only the ‘_’ is added after the calling the function add2 for value of sum.



My Personal Notes arrow_drop_up

Recommended Posts:



    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.