Introduction To Functional Programming With Scala

Reading Time: 3 minutes

1. Overview Of Functional With Scala

In this blog, we will learn about functional programming concepts with Scala as a programming language.

2. What is Functional Programming?

Functional programming is a programming paradigm that uses functions as the central building block of programs. In functional programming, we strive to use pure functions and immutable values.

First of all, each function must be a pure function. But what does that mean exactly? To answer this question we first need to understand what a function is. In layman’s terms – a function is something which when fed some input data, produces an output. In Scala, it may look like this:

def fun(data: Int): Int = data * 2

Referential transparency

The above function, when called with some specific argument, will always return the same value for such input, in this case, it will multiply it by 2. This means that it’s deterministic, therefore the same input always returns the same output.  It’s worth noting that such a function call could be easily replaced by its resulting value without changing the behavior of the entire program. Such a property is called referential transparency.

Lack of side effects

The other important thing is the lack of side effects. What does this mean exactly?

Nowadays, when imperative languages still rule the world, nearly every function has some side effects. Changing the contents of the passed in the collection, altering an external variable, throwing an exception, etc – are all examples of side effects. When they get eliminated, testing such functions is truly an easy task, since the behavior is fully deterministic.

Totality of a function

Another crucial aspect is the totality of a function, which means that it should always return a value for every possible input, even if an error has occurred.

This can be achieved by introducing types guaranteeing optionality or wrapping both success and error values (luckily Scala’s standard library comes already equipped with such classes),  so there is no need for dangerous and unpredictable exception-based handling.

These kinds of functions can be easily combined, something which is called functional composition.  Composing two functions is done simply by passing the result of the first function call as an argument to the second function. This is how it’s done in Scala:

def f(data: Int): Int = data + 1
def g(data: Int): Int = data * 2
val fWithg = f _ compose g _
fWithg(2) // returns 5
// it’s equivalent to:
f(g(2)) // returns 5 as well

There are also different ways to combine functions. Higher order functions can for example accept as an argument different functions, or even return one. This allows for even better flexibility when writing code

def functionAsArgument(name: String, prettify: String => String): String = s"Hello from ${prettify(name)}"
functionAsArgument("Rick", name => s"Pickle $name")

def functionReturningFunction(prettify: String => String): String => String  = 
  (name: String) => functionAsArgument(name, prettify)

val picklify = functionReturningFunction(name => s"Pickle $name")

In Scala, it’s also possible to create a  new function from another one partially by applying specific arguments. Example implementation of totality:

def functionAsArgument(name: String)(prettify: String => String): String = s"Hello from ${prettify(name)}"

val picklify1 = functionAsArgument1(_: String)(name => s"Pickle $name")

Immutability Functional

  1. Immutability means unable to change.
  2. In the Functional Programming world, we create values or objects by initializing them.Then we use them, but we do not change their values or their state.
  3. Thus, immutable objects are more thread-safe than mutable objects. We’ll study how Scala enforces immutability in a later section.

Pure Functions

A pure function has two key properties:

  1. It always returns the same value for the same inputs.
  2. It has no side effects. A function with no side effects does nothing other than simply return a result.
  3. Any function that interacts with the state of the program can cause side effects.
  4. The state of the program can be mutable objects, global variables, or I/O operations, for example.

 3. How Is Scala Functional?

Scala is a functional language in the sense that every function is a value. The fundamental concept of functional Scala is functions act as first-class citizens in Scala.

There are various functional constructs that we can create in object-oriented programmings.

But in a functional programming, these occur naturally and arise quite often. In object-oriented programming, we somewhat rarely encounter these phenomena.


In this blog, we introduced functional programming in Scala and explained how exactly Scala is functional and why we might use it.


Written by 

My name is Harshal Dubey. I am working as a Software Intern in AI/ML Studio at Knoldus. My hobbies are playing volleyball, football and Travelling.