Currying Function in Scala

currying_function

Learning Scala involves learning lots of new concepts and syntax. In this blog post you will learn one of the important concept know as Currying function in scala.

Definition

Definition : Currying is the technique of transforming a function with multiple arguments into a function with just one argument.

Let us understand with an example program

def multiply(num1:Int,num2:Int):Int={
    num1+num2;
}

In the above example we have a function by name “multiply” which takes two arguments i.e num1 and num2

Since scala supports curried functions, we can turn into curried version by simply separating out the arguments as shown below.

Short hand Curried function

def multiply(num1:Int) (num2:Int):Int={
    num1+num2
}

Long hand Curried function

The above curried function is short hand for the code shown below:

def multiply(x: Int): (Int => Int) = {
  (y: Int) => {
    x + y
  }
}

Let me explain the above code, If you observe the above code the return type of a function is another function i.e after “:” what you see is a return type hence the function returns (num2:Int):Int

Let us invoke the function

scala> val functionOutput = multiply(2)


Output

functionOutput: Int => Int = <function1>

Invoking on a return function

scala> val output = functionOutput(3)

Output:

output: Int = 5

Summary:

  1. Currying is the technique of transforming a function with multiple arguments into a function with just one argument.
  2. The single argument is the value of the first argument from the original function.
  3. The function returns another single argument function. This in turn would take the second original argument and itself return another single argument function. This chaining continues over the number of arguments of the original. The last in the chain will have access to all of the arguments and so can do whatever it needs to do.

Download code

Reference documentation


About the course

The Apache Spark and Scala Training program is our in-depth program  designed to empower professionals to develop relevant competencies and accelerate their career progression in Big Data technologies through complete Hands-on training on Spark and Scala.

Learn Apache Spark by developing industry standard projects by following Agile methodologies  | Apache Spark Training with Project

Related Post
Aggregation using collect_set on Spark DataFrame In this blog post you will learn how to use collect_set on Spark DataFrame and also how to map the data to a domain object. Introduction collect_set...

Naveen P.N

12+ years of experience in IT with vast experience in executing complex projects using Java, Micro Services , Big Data and Cloud Platforms. I found NPN Training Pvt Ltd a India based startup to provide high quality training for IT professionals. I have trained more than 3000+ IT professionals and helped them to succeed in their career in different technologies. I am very passionate about Technology and Training. I have spent 12 years at Siemens, Yahoo, Amazon and Cisco, developing and managing technology.