Scala Currying Functions

Currying converts a function with multiple arguments into a series of a functions with single argument. This article provides an introduction to Scala Currying Functions.

Scala Currying Functions is often confused with Scala Partially Applied Functions. Both currying and partial application enables creation of derived functions with smaller number of arguments. Key difference is that in currying we get the next single argument function in a series of functions whereas in partial application we get a function with smaller number of arguments.

Let us look at a simple example of curried function.

object ScalaCurryingTest {def add(x:Int)(y:Int)(z:Int) : Int = {x + y + z}def main(args: Array[String]) {println(add(10)(20)(30))val curried = add(10) _println(curried(20)(30))}}

In this example we define a curried function add with multiple parameter lists. When the curried function is invoked it returns the next single argument function in the series of functions.

Continue to read more Scala Tutorials.