Beginners Guide to Functional Programming With Scala

Neha Sudini
3 min readNov 10, 2020

By: Neha Sudini, Leoul Tadesse

Source: modernescpp

Functional programming is a declarative programming language that incorporates mathematical function styles. These mathematical functions use conditional expressions and recursion to perform computations. There are impure and pure functional languages. Scala for example is an impure language that supports both object-oriented and functional programming.

If you have heavily used object-oriented programming, you will find some pretty notable differences between imperative languages like Java to functional languages such as Scala. If you are new to Scala and have experience with object-oriented programming, you’ll quickly see that it is much like Java and is even interpreted to bytecode which is then executed by the JVM that Java uses. Scala has emerged as one of the most powerful alternatives to Java with features such as currying, pattern matching, immutability that add to its increasing popularity.

Below is a compilation of a few distinctions and advantages of Scala. To help highlight the fundamental differences, there are code snippets of the featured Scala code and its Java equivalent code (if applicable.)

Functional programming is extremely concise. Implementations that take several lines of code in imperative languages can be implemented in a single line in functional programming languages such as Scala. The following are two examples that show the difference between Java and Scala in Object creation.

Java is an imperative language where every programming logic is explicitly written line by line. On the other hand, Scala is a declarative language and a lot of implementation details are hidden and a programmer simply writes conditions on the outcomes and higher-order functions accomplish it. For example, reading input files in Scala can be implemented in a single line because of its declarative nature while it takes several lines in Java.

Digging more into high-order functions, a very powerful one that Scala supports is the map function. Map transforms one collection to another of the same type and applies a given function or condition across the entire collection. The below example uses a user-defined function, but you can also use an anonymous function which will be shown soon. The equivalent Java code requires an iteration with a typical loop structure.

If you’ve worked with Java, you’ve definitely faced thread-safety issues but Scala is designed to reduce these risks. Scala and Java both support immutability and mutability; however, Scala has inherently immutable objects for the benefit of removing potential bugs from creeping into the program. The following Scala code doubles list1 and stores this new collection into another variable. The map function uses an anonymous function this time, which is defined by the short notation of ‘_’ and nicely cuts down some lines of code. The equivalent Java code directly modifies the existing list because of mutability.

Unlike Java, Scala supports a feature called lazy evaluation where expression computations are delayed until they are accessed the first time. This feature makes Scala very efficient because intensive tasks don’t have to be executed right away so they are until they are needed.

In this example, the variable bigtask is initialized on the first line which is still not computed yet. The variable bigtask is evaluated at the end and only then it is actually computed and the value is returned.

scala> lazy val bigtask = println(“big task”)bigtask: Unit = <lazy>scala> bigtaskbig task

While there are several advantages to Scala, like all programming languages there are some cons. For instance, it takes longer to compile Scala source code compared to Java. One feature of Scala that might explain this is that it supports type inference and the compiler has to figure out the implicit variable types during compile time. Another is that Scala is not backward compatible.

However, overall, Scala is a highly functional language that is extremely concise, logical, and powerful. It results in a cleaner and more well-organized code which ultimately increases code productivity. Scala is also great for data analytics with support from tools like Apache Spark and for developers who want to apply functional programming practices in other languages.

There is definitely a learning curve with Scala, but hopefully, this guide serves you with a basic understanding of some of its unique features to get you started. Whether it’s Scala, Haskell, or some other functional programming language, it is definitely worth the challenge to become a functional programmer.

--

--