Scala Currying Function – Example & Partially Applied Function

Free Scala course with real-time projects Start Now!!

Today, we will learn about Scala currying functions. Moreover, we will discuss advantages of currying in Scala Programming Language and how to call a scala currying function. Along with this, we will study Scala Currying vs partially applied functions. In addition, we will look at an example of Scala Currying Function.

So, let’s see the Scala Currying Function Tutorial.

What is Scala Currying Function?

Through Scala curry function, we can split a list with multiple parameters into a chain of functions-each with one parameter. This means we define them with more than one parameter list.
Follow this link to know about Scala String Method with Syntax and Method

A Syntax of Scala Currying Function:

We use the following syntax to carry out currying:

def multiply(a:Int)(b:Int)=a*b

Another way we can do this is:

def multiply(a:Int)=(b:Int)=>a*b

Calling Scala Function

To make a call to Scala function, then, we call it passing parameters in multiple lists:

Partially Applied Functions

An important concept to discuss here is partially applied functions. When we apply a function to some of its arguments, we have applied it partially. This returns a partially-applied function that we can use later. Let’s take an example.

scala> def multiply(a:Int)(b:Int)(c:Int)=a*b*c
multiply: (a: Int)(b: Int)(c: Int)Int
scala> var mul=multiply(2)(3)(_)
mul: Int => Int = $$Lambda$1122/1609544540@4f92ded0

Here, the underscore is a placeholder for a missing value.

scala> mul(4)
res11: Int = 24

Let’s Explore Scala Functions in detail
Well, this works too:

scala> var mul=multiply(2)(3)_
mul: Int => Int = $$Lambda$1173/256443308@207ceea4
scala> mul(4)
res12: Int = 24

Example of Scala Currying Function

Example – 1
Let’s begin with Scala Currying Function example.

scala> class Add{
| def sum(a:Int)(b:Int)={
| a+b}
| }
defined class Add
scala> var a=new Add()
a: Add = Add@53cba89f
scala> a.sum(3)(4)
res4: Int = 7

Example – 2
Remember the other piece of syntax we looked at ? Let’s try defining a function that way, but with three arguments.

scala> class Concatenate{
| def strcat(s1:String)(s2:String)=(s3:String)=>s1+s2+s3
| }
defined class Concatenate
scala> var c=new Concatenate()
c: Concatenate = Concatenate@5d55eb7a
scala> c.strcat("Hello")("World")("How are you?")
res7: String = HelloWorldHow are you?

Do you know about Scala Exceptions and Exception Handling

Advantages of Currying Function in Scala

Here, we will discuss the advantages of Currying in Scala, let’s discuss them:

  1. One benefit is that Scala currying makes creating anonymous functions easier.
  2. Scala Currying also makes it easier to pass around a function as a first-class object. You can keep applying parameters when you find them.

So, this was all about Scala Currying Function. Hope you like our explanation.


Hence, using the concept of partial functions, we use curry functions in Scala. This lets us split a list of multiple parameters into a chain of functions. Drop your queries in the comments.

Related topic – Scala File i/o: Open, Read and Write a File in Scala

For reference

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

follow dataflair on YouTube

1 Response

  1. Abhijat says:

    is there any real world example of scala currying?

Leave a Reply

Your email address will not be published. Required fields are marked *