02: Scala FP basics: First-class function, higher order function, statements Vs expressions, immutability & recursion

This extends Scala Functional Programming basics – pure functions, referential transparency, side effects, etc.

Q1. What is a first-class function?
A1. A first-class function is a function that can be treated like a value.

1) Can be assigned to a variable as a value can be assigned to a variable.

Same as below as return types after “:” are inferred in Scala.

2) Can be passed around as arguments to other functions as values can be passed around.

3) Can be returned from another function as values can be returned from a function.

Q2. All functions in Scala are first-class functions. True or False?
A2. True.

Q3. What is a higher order function?
A3. A higher-order function is a function that can do one of the below two things.

1) takes one or more functions as arguments.

2) returns a function as a result

Q4. What is the benefit of passing the functions around?
A4. It gives you abstraction.

Without higher order function

There is no code reuse. Separate functions are defined to compute add addAllNumbers(..), addOddNumbers(..) and addGt3Numbers(..). If you have additional requirements, then you need to define more functions like addPrimeNumbers(..), addEvenNumbers(..)

With a higher order function

The common behaviour can be abstracted out to a function named addNumbers(..) that takes a function f (i.e. Int => Boolean) as an argument.

Note: x => true, x => x % 2 != 0, etc are anonymous functions. That is functions without names.

Q5. Statements Vs Expressions, and what does functional programming use?
A5. Functional Programming is all about mathematical expressions. Statements mutate values as in

Whereas expressions are all about immutability as in

I can hear you asking how can we write programs without mutating variables? The FP favours immutability over mutating a variable.

1) copying original object & mutating.

2) favouring recursion & tail recursion over looping. Recursion is function calling itself. Using recursion you don’t need a mutable state while solving some problems.

3) The FP combinator higher order functions like map(..), flatMap(..), filter(..), foldLeft(…), etc

Simple mutable code with “var” & loop example


Simple immutable with recursion example

No declaration of immutable variable “var” in the below code using recursion.


Recursion can be inefficient for very large collections as it can throw Stack Overflow errors as it keeps the values of previous iterations in the stack & computes the final value in the last iteration. The tail recursion is more efficient for the larger size collections.

Tail recursion is more efficient


Using the foldLeft() function of the List class


It can also be rewritten with place holder “_” as in _ + _

Imperative code that mutates the original List

Please note that the code is simple, but there are lots of println(…) statements to show that the original object reference is mutated & returned or a copied object is returned with a new salary. Pay attention to the object reference ids in the output as in “Employee@6e0be858” & “before mutation = 692404036“.


Functional code that copies the original List


Note: In Scala, the case class construct gives you the copy method for free along with toString, equals, hashCode, etc.


Functional code using recursion

Many problems can be solved by recursively calling a function until a certain condition is met. It can also solve lots of problems you can’t solve with loops.

Note: “head” is the first element in the list, and “tail” is the all the remaining elements in the list. So, it computes the salary for the first element in the list & then recurse (i.e. call the same function again) with the remaining elements until an exit condition is reached, which is the empty list.


300+ Java Interview FAQs

800+ Java Interview Q&As