Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

scala

What are Multiple Parameter Lists (Currying) in Scala?

Behzad Ahmad

Currying is a technique employed by functional programming languages like Scala. It breaks down the calculations of a single function with multiple parameters into multiple partial functions which only have a 2-tuple of parameters.

These two parameters include a single parameter passed to the original function, as well as another function that takes a single argument and a function. In this way, we can form a chain of such functions based on the number of parameters included in the original function.

Syntax

strcat("arg1")("arg2")

Example

We can define the currying function with multiple parameter lists as shown below:

def strcat(arg1: String)(arg2: String) = arg1 + arg2

Code

object temp {
   def main(args: Array[String]) {

      val arg1 : String = "Welcome to "
      val arg2 : String = "Educative"
      println( "String1 + String2 = " +  strcat(arg1)(arg2) )
   }

   def strcat(a1: String)(a2: String) = {
      a1 + a2
   }
}

Output

String1 + String2 = Welcome to Educative

Explanation

In the above piece of code, we make an object named temp. Next, we make two variables of string values and store some lines in them. After that, we call println to print both strings. For this, we call the strcat function and pass both the arguments.

RELATED TAGS

scala

CONTRIBUTOR

Behzad Ahmad
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring