WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Scala · Intermediate · question 35 of 100

Explain how to handle multiple parameter lists in Scala and their benefits.?

📕 Buy this interview preparation book: 100 Scala questions & answers — PDF + EPUB for $5

In Scala, you can define a function with multiple parameter lists, also known as currying. To do this, you just need to split the single parameter list into multiple lists. When calling the function, you also need to pass the arguments in multiple lists separated by parentheses.

Here is a simple example demonstrating a function with multiple parameter lists:

def add(a: Int)(b: Int): Int = a + b

Now, to call this function, you would use the following syntax:

val result = add(3)(4) // result will be 7

Benefits of multiple parameter lists:

1. Partial application and function currying:

Multiple parameter lists allow you to partially apply some arguments to a function and later apply the remaining arguments. This is useful when you want to create specialized functions from a more generic one. Let’s take an example:

val addThree = add(3) _ // addThree is a function that adds 3 to its argument
val result1 = addThree(4) // result1 will be 7
val result2 = addThree(5) // result2 will be 8

Here, we partially applied the first argument to the ‘add‘ function and created a new function ‘addThree‘ that takes a single argument and adds 3 to it.

2. Implicit parameters:

Some functions might require certain parameters that can be automatically provided by the compiler. When a parameter is marked as ‘implicit‘, the compiler will search for an implicit value of that type within the scope and use it when calling the function. By dividing parameters into multiple lists, you can create an implicit parameter list distinct from the non-implicit parameters. Here’s an example:

case class Config(precision: Int)

// Sum of squares with an implicit Config parameter to define the precision
def sumOfSquares(a: Double, b: Double)(implicit config: Config): Double = {
  val factor = Math.pow(10, config.precision)
  val result = a * a + b * b
  Math.round(result * factor) / factor
}

implicit val defaultConfig: Config = Config(2)

val result = sumOfSquares(3.123, 2.562) // result will be 17.55, using the defaultConfig implicitly

In this example, we created a ‘sumOfSquares‘ function with an implicit config parameter that controls the precision of the output. By declaring an implicit ‘Config‘ value in the scope, the function call uses it automatically.

3. Type inference:

Multiple parameter lists can improve the type inference of Scala’s compiler. Sometimes, type information from one set of parameters can help the compiler infer the types of other parameters. For example, consider the following function:

def foldLeft[B](list: List[Int], z: B)(op: (B, Int) => B): B = {
  list.foldLeft(z)(op)
}

val list = List(1, 2, 3, 4)
val result = foldLeft(list, 0)(_ + _) // result will be 10

Here, we have a custom ‘foldLeft‘ function tailored for lists of integers. The generic type ‘B‘ can be inferred by the type of the ‘z‘ parameter from the first parameter list. The second parameter list contains a function with input types derived from the types of ‘z‘ and the list’s elements type.

In summary, currying and multiple parameter lists in Scala bring several benefits, such as partial application, improved type inference, and separation of implicit and non-implicit parameters. These features help create more maintainable, flexible, and easier-to-understand code.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Scala interview — then scores it.
📞 Practice Scala — free 15 min
📕 Buy this interview preparation book: 100 Scala questions & answers — PDF + EPUB for $5

All 100 Scala questions · All topics