02: Q6 – Q12 Scala FP currying, closures & partial functions interview Q&As

This extends Q1 – Q5 Scala Functional Programming basics interview questions & answers

Q6. What is a curried function in Scala?
A6. Currying is the technique of transforming a function with multiple arguments into a function with just one argument, and the other arguments are curried.

Currying is when you break down a function that takes multi-group parameters into a series of functions that take part of the arguments. Here is an example:

“(x: Int, y: Int)” is split into “(x: Int)(y: Int)”.

Output:

“_” is an anonymous function place holder parameter. The above curried function was written the shorter way, and can be written as shown below by converting (Int, Int) => Int to Int => (Int => Int)

Output:

The curried function can be simplified as shown below

Scala REPL example 1:

(Int,Int) => Int. A function takes two “int” values and returns an “int” value.

Note: _ after addCurried(5) is an eta expansion“. It converts methods into functions (i.e. Function1, Function2, etc).

So, you can think of addCurried(5) _ to be converted to

You can try the full code as:

Scala REPL example 2:

Int => (Int => Int). A function that takes an “int” value, and returns another function that takes an “int” as input and returns an “int”.

Q. How will you write a generic function to convert any binary functions as shown below into a curried function?

A. Basically, you need function that converts (Int, Int) => Int to Int => (Int => Int).

You can now apply it as shown below:

Q7. What is a closure in Scala?
A7. A closure is a function, whose return value depends on the value of one or more variables declared outside this function. These are also known as free variables.

Closure = function + environment

A function has an implicit environment that can be used to access variables lexicographically like “more”.

A closure will have one or more free variables as variable “more” shown below. If “more” is declared with “val“, which means it is immutable, and becomes referentially transparent & will be a pure function. If “more” is defined as a “var”, then addCurried(..) becomes an impure function.

Example 1:

Understanding of closures is important to learn the concepts like functors & monads.

Example 2:

Example 3: REPL

Q. What is the benefit of a closure?
A. As demonstrated below it is pointer to an environment that has data as in the “price” & “discount” Maps to look up for price & discount based on the “productId” and also to the function body, which is the code. This is an analogy to the object oriented programming that has access to data via instance variables & the behaviour via methods.

Calculate product discount amount for a given productid. The “price” & “discount” are free variables defined within the function, but outside the closure – ( productId, price(productId) * discount(productId) / 100).

Q8. Can you apply the function place holder “_” (i.e. underscore) to the following code snippet?

A8. Scala uses the underscore to mean different things in different contexts, but you can think of it as an unnamed wildcard. A place holder makes a functional syntax more concise as shown below.

Q9. What will be the output of the following partial function code snippet?

A9. You can partially apply a function with an underscore, which gives you another function. The “_” here stands for any argument.

Output:

Q10. Can you write a capitalize function in Scala that takes variable number of Strings input as shown below?

capitalize(“john”, “sam”, “peter”)
capitalize(“apple”, “pears”)

A10. The special syntax with “*” is shown below to take variable length arguments in Scala.

Output:

Q11. Can you write a recursive function in Scala to calculate the factorial?
A11.

First Pass: 4 * factorial(3)
Second Pass: 3 * factorial(2)
Third Pass: 2 * factorial(1)
Fourth Pass: 1 * 1 [Exit Condition is Reached]

The result is: 4 * 3 * 2 * 1 * 1 = 24

Q12. What is tail recursion? Can you write a tail recursion function Scala to calculate the factorial?
A12. A recursive call to be tail recursive, the call back to the function must be the last action performed in that function. In the above example, since the result of each recursive call is being multiplied by the next recursion call, the recursive call is NOT the last action performed in the function, hence NOT a tail recursion.

To make it a tail recursion, the “factorial” function needs to take two arguments. The “interimResult” argument stores the intermediate result, and it is initialized from 1.

Q. What is the benefit of tail recursion?
A. Since recursion takes place in the stack memory, and in the recursion example the result of each call must be remembered (e.g. 4 * 3 * factorial(2) …), each recursive call requires an entry on the stack until all recursive calls have been made. This makes the recursive call more memory intensive. The “tail recursion” stores the result as an intermediate result.

Q. Since in Scala you can declare a function within another function, is it possible for the factorial method to take a single argument as in factorial(4)?
A. Yes.


800+ Java Interview Q&As Menu

Top