What Does `(...)(...)` Mean In Scala Code?

9 minutes read

In Scala code, (...)(...) is a syntactic construct known as multiple parameter list or currying.


In the context of Scala, a function can have multiple parameter lists, each enclosed within parentheses. When a function is defined using multiple parameter lists, it allows you to partially apply parameters to the function, resulting in a function with fewer parameters.


For example, consider the following function definition:

1
def add(x: Int)(y: Int): Int = x + y


Here, the add function takes two parameters x and y, but they are split across two parameter lists. This allows you to partially apply the function by providing only the first parameter list, resulting in a new function that takes only one parameter.


Here's an example of partially applying the add function:

1
2
3
val add2: Int => Int = add(2) // Partially applied function
val result = add2(3) // Calling the partially applied function
println(result) // Output: 5


In this example, add(2) returns a partially applied function add2, which takes a single parameter y. The add2 function can be called separately with the remaining parameter to get the desired result.


This technique gives you more flexibility in composing functions and creating reusable code. It can be especially useful in functional programming paradigms where functions are treated as first-class citizens.

Best Scala Books to Read in 2024

1
Functional Programming in Scala, Second Edition

Rating is 5 out of 5

Functional Programming in Scala, Second Edition

2
Programming in Scala Fifth Edition

Rating is 4.9 out of 5

Programming in Scala Fifth Edition

3
Programming Scala: Scalability = Functional Programming + Objects

Rating is 4.8 out of 5

Programming Scala: Scalability = Functional Programming + Objects

4
Hands-on Scala Programming: Learn Scala in a Practical, Project-Based Way

Rating is 4.7 out of 5

Hands-on Scala Programming: Learn Scala in a Practical, Project-Based Way

5
Learning Scala: Practical Functional Programming for the JVM

Rating is 4.6 out of 5

Learning Scala: Practical Functional Programming for the JVM

6
Scala Cookbook: Recipes for Object-Oriented and Functional Programming

Rating is 4.5 out of 5

Scala Cookbook: Recipes for Object-Oriented and Functional Programming

7
Functional Programming in Scala

Rating is 4.4 out of 5

Functional Programming in Scala

8
Programming in Scala

Rating is 4.3 out of 5

Programming in Scala


How to define default values for named arguments in Scala?

In Scala, you can specify default values for named arguments by using the "default" operator = in the function definition.


Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
def greet(name: String = "John", age: Int = 30): Unit = {
  println(s"Hello $name, your age is $age")
}

// Calling the function without arguments
greet() // prints: Hello John, your age is 30

// Calling the function with one named argument
greet(name = "Alice") // prints: Hello Alice, your age is 30

// Calling the function with both named arguments
greet(name = "Bob", age = 25) // prints: Hello Bob, your age is 25


In the greet function definition, the name argument has a default value of "John" and the age argument has a default value of 30. These values are used when the function is called without providing explicit values for these named arguments.


You can then call the function with named arguments, specifying values for the named arguments you want to override.


How to pass arguments to an anonymous function with multiple parameter lists in Scala?

To pass arguments to an anonymous function with multiple parameter lists in Scala, you can use the "currying" technique.


Here's an example:

1
2
3
4
5
val multiply: (Int) => (Int) => Int = (x) => { (y) => x * y }

val result = multiply(5)(3)

println(result) // Output: 15


In this example, we have defined an anonymous function called multiply that takes two integer arguments, x and y, and returns their product. The function is defined using currying, meaning it has multiple parameter lists.


To pass the arguments to the function, you can use parentheses to apply the arguments one by one. In the example above, we first pass 5 to the function, which returns another function that takes 3 as an argument. We then apply 3 to the returned function, which gives us the final result of 15.


How to use multiple parameter lists for type inference in Scala?

In Scala, you can use multiple parameter lists to enhance type inference. When you have multiple parameter lists, type inference can work better because it can infer certain types from one parameter list and use them in subsequent parameter lists. Here is an example to illustrate how to use multiple parameter lists for type inference:

1
2
3
4
def add[A](a: A)(implicit numeric: Numeric[A]): A = numeric.plus(a, numeric.zero)

val result = add(3)(using Numeric.IntIsIntegral)
println(result) // Output: 3


In this example, the add function has two parameter lists. The first parameter list has a single parameter a of type A, and the second parameter list has an implicit parameter numeric of type Numeric[A]. The Numeric type class provides operations like addition (plus) and zero for numeric types.


When calling the add function, you specify the value 3 in the first parameter list. The type A is inferred as Int from the value 3 because it is an Int literal. In the second parameter list, an implicit value of Numeric.IntIsIntegral (which provides the required Numeric[Int] instance) is used for type inference. The type inference engine incorporates the inferred type Int from the previous parameter list and applies it to the implicit parameter.


Finally, the add function adds the value 3 to numeric.zero, which is 0 for Int. The result is 3, which is then printed.


Note that in this example, type inference is enhanced by using an implicit parameter in the second parameter list. This is just one way to utilize multiple parameter lists for type inference in Scala. The specific technique to use depends on the requirements of your code.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To find the mean of an array in MATLAB, you can use the built-in function mean(). Here is an example code snippet that demonstrates its usage: % Define an example array array = [5, 10, 15, 20, 25]; % Calculate the mean using the mean() function array_mean = m...
Exception handling in Scala is similar to other programming languages like Java and C++. Scala provides various constructs to handle exceptions and gracefully recover from them. Here are some important points to consider when handling exceptions in Scala:Excep...
Working with collections in Scala allows you to perform various operations on a group of elements. Scala provides a rich set of collection classes and methods that make it easy to work with data in a functional and efficient way. Here are some key points to un...