This series is all about the taste of Scala.
It is best suitable for all the newbies in Scala.
In the previous part, we covered the:
- Classes, constructors, inheritance, and abstract classes
- Traits and anonymous classes
- Singleton objects and companion objects
- Case classes and case objects
This article will cover the functional nature of the Scala language.
The first fundamental concept of functional Scala is pure functions.
Here is a very strict definition of purity:
- It returns the same result if given the same arguments
- It does not cause any observable side effects
Functions as First-Class Citizens in Scala
In Scala, functions are first-class citizens because we can do the following things with them:
- We can use functions as values or like normal variables. This can be demonstrated as:
- We can assign an anonymous function i.e. a function literal to a variable. This can be demonstrated as:
- We can pass one or more functions as parameters to another function.
- We can return a function from another function
Let’s dive into the details of the last two points mentioned above. These two functionalities are provided by high order functions in Scala.
High Order Functions
A high order function takes other functions as a parameter or returns a function as a result. In other words, we can say a function that works with function is called high order function.
High order function allows you to create function composition, lambda function or anonymous function, etc.
An example of passing a function as a parameter in a function is demonstrated below:
An example of returning a function from a function is demonstrated below:
There are some classic HOFs we use a lot with collections are
filter. Let’s take them in turn as examples.
The map method
map method takes a function as an argument and returns a new collection containing the applications of the function on every element. It can be demonstrated as:
The flatMap method
flatMap method takes a function as an argument, applies it to every element in the collection, and returns a new collection for every element. The
flatMapmethod is essentially a combination of the
mapmethod being run first followed by the
flattenmethod. It can be demonstrated as:
The filter method
filter method takes a function as an argument and returns a new collection containing just the elements for which the function returns true. It can be demonstrated as:
Thus instead of iterating through a collection and manually constructing the values, we are manipulating the existing collections through their high order functions.
Currying is the process of converting a function with multiple arguments into a sequence of functions that take one argument. Each function returns another function that consumes the following argument.
An example of curried function is demonstrated below:
Partially Applied Functions
The partially applied functions are the functions which are not applied on all the arguments defined by the stated function i.e., while invoking a function, we can supply some of the arguments and the left arguments are supplied when required.
An example of partially applied function is demonstrated below:
Scala provides tail recursion optimizations to reduce the risk of a stack overflow error caused by recursive functions.
A tail-recursive function an optimized recursive function whose recursive call is the last operation performed by the function, and no operations need to be saved when the function returns.
@tailrec annotation to check if the recursion is tail recursive or not.
An example of tail-recursive function is demonstrated below:
If you want to get started in functional programming, enter Scala.
Most popular programming languages of today started out object-oriented, and are now integrating more and more features of functional programming as demand is rising. Whereas Scala has been poised to integrate both from day one.
Scala provides you with enough object-oriented programming that you’ll feel like you’re on familiar ground. At the same time, it’s an excellent way to get to know functional programming at your own pace.
Stay tuned for our next part of Scala Beginner Series where we will cover the advanced concepts of Scala. You don’t want to miss it!