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 Function object 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 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
// 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.
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
// 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)); } } |
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 function object 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 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)); } } |
Output:
34
Here, only the ‘_’ is added after the calling the function add2 for value of sum.
Recommended Posts:
- Scala | Partially Applied functions
- Scala | Functions Call-by-Name
- Using anonymous functions with the map method in Scala
- Scala | Functions - Basics
- Anonymous Functions in Scala
- Scala | Nested Functions
- Partial Functions in Scala
- Higher Order Functions in Scala
- Scala Tutorial – Learn Scala with Step By Step Guide
- Scala Int Compare Method With Examples
- Scala Int CompareTo Method With Examples
- Scala Double +(x: String) Method With Examples
- Scala Listset apply() method with examples
- Scala - String Methods with Examples
- Scala short <(x: Short): Boolean
- Scala short <(x: Char): Boolean
- Scala Extractors
- Scala String indexOf(String str) method with example
- Scala String contentEquals() method with example
- Scala Keywords
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

