Scala Currying Function – Example & Partially Applied Function

1.  Objective

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.

Scala Currying Function - Example & Partially Applied Function

Scala Currying Function – Example & Partially Applied Function

2. 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

3. Calling Scala Function

To make a call to Scala function, then, we call it passing parameters in multiple lists:
multiply(3)(4)

4. 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

5. 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

Scala Quiz

7. 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.

8. Conclusion

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

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.