Curried Functions in Simple Words in Scala

In this blog post, we will take a look at curried functions in Scala with the help of an example.

What are Curried Functions?

Currying is a technique or a process for modifying a function in Scala. This function converts several parameters into a single argument function. Curried functions have multiple parameter lists. It is widely used in a wide range of functional languages.

Let’s take a look at an Example:

Normally, we write a function and it looks like this:

``def simpleMultiply(a: Int, b: Int): Int = valueOne * valueTwo ``

That means we declare a function with all the arguments needed inside a single parameter list.

However, when currying, we can divide this parameter list into multiple parameter lists.

``def curriedMultiply(a: Int)(b: Int): Int = valueOne * valueTwo ``

For example, we could divide the parameter list into multiple lists, each of which only accepts one parameter. The function body, however, remains the same in both cases.

Also, instead of passing all of the arguments together, we pass them as required by the curried function.

``````val result = curriedMultiply(2)(10)

val result1 = curriedMultiply(2) _
val result2 = result1(10) ``````

It’s worth noting that when we declare the curried function, we get two traditional function invocations back-to-back instead of just one.

Let’s see the return type of multiplyCurry.

curriedMultiply(valueOne: Int)(valueTwo: Int)

The first function takes the integer argument and returns the Function Value for the second function.

curriedMultiply(valueOne: Int)(valueTwo: Int)

This second function takes the integer parameter and applies to the Function Value.

The output of the curried function is very similar to the output of writing a function that returns a Function Literal.

Also we see in case of this, we have partially applied the function by passing underscore (_) as a proxy to the second parameter list. Here, the variable “result1” refers to the second Function Value. We have confirmed that by applying the same argument 10 and comparing the result of “result2” with “result”, and they both give the same value as an output.

Conclusion

Thank you guys for making it to the end of the blog I hope you gained some knowledge about currying in Scala. Currying is mainly a design technique for creating functions. It’s something we usually accept as a good to know the concept.

We generally use it in conjunction with Partially applied functions when multiple parameters are required. It frequently aids in reducing code duplication.

Reference

For more detailed information about currying in Scala, you can use this link: https://docs.scala-lang.org/ To learn about other Scala topics like recursion, kindly check here.

Written by Pallav Gupta

Pallav is working as Software Consultant at Knoldus Inc. He is a technology enthusiast and a keen learner with more than 1.5 years of experience. He works as a Quality Catalyst in the organization. He has a good understanding of programming languages like Java and Scala. He likes to listen to songs and playing table tennis.