Functional Programming with Scala: an Intro

Functional Programming with Scala: an Intro

If you are curious about Functional Programming this post describes a couple of important aspects of it as they work in Scala.

Conventional programs establish conceptual limits about how problems must be modularized. Functional languages minimize such limits.

— John Hughes, Why Functional Programming Matters

As John Hughes points out, the problems we solve with software are becoming more complex with the passing of time. They require a more practical way to be tested and a more efficient approach to writing them. The Functional Programming paradigm helps greatly in both these respects.

Even more importantly, this paradigm is a natural for parallelism. For several years now gains in CPU raw speed have been modest. Nevertheless, systems have been getting faster by adding more cores which run in parallel.

Why Scala?

Scala is a language that features full support of Functional Programming as well as the Object-Oriented Paradigm. It is one of the most widely utilized languages by the Functional community, up to par with F#, and Haskell, Clojure, among others. Scala is executed on a JVM and it plays nice with Java, Groovy, Clojure, etc.

Pure Functions

A pure function depends solely on its inbound parameters and should only return a specific result without mutating an outer condition within its reach. A good example of a pure function is is simply based on the parameter it receives and it offers a result.

Non-pure Functions

Non-pure functions allow external/internal context modification.
Pure functions are easy to test and predict. However, sometimes it’s necessary to do something that modifies state, like writing a file, or updating a table within a database. That’s why non-pure functions exist. For example,

High Order Functions

One of the most powerful traits of Functional Programming are high order functions. They can take values or functions and return other values or functions,

  • take a function, return a value
  • take a value, return a function
  • take a function, return a function

These functions not only shorten the programs, but also help produce cleaner code.

Immutability

Immutability refers to the data within an object that cannot be modified once it has been created.

Of course, it’s not always possible to use immutable states, but designing the programs based on this premise is always highly recommended.

These examples contrast the advantages offered by Scala against an Object-Oriented language like Java,

Monads in Scala

A Monad is an interface that simply specifies one form for data composition.

A Monad should be ruled by certain laws. Scala is not as strict with these laws, and its focus is more practical than anything. From the language standpoint, the only traits that a Monad should have are the high-order functions flatMap and map. These provide the tool to compose the data they contain, through the application of a function.

Maybe it’s easier to explain it with an example,

Imagine for a moment that every collection we know are a Monad (in Scala terms). Then, we are able to compose the internal data in this collection simply by applying a function through map or flatMap.

Conclusion

This post simply describes some of the most important aspects of Functional Programming with Scala. There are many more topics to learn. For example, language-specific Monads (Option, Future & Try) or “for-comprehension”and ”apply” method to simplify programs.

Also, learn more about some of the most widely used frameworks like Play!, Finagle, and Akka.

I sincerely hope to have sparked your interest in Scala and Functional Programming.

Focus Mode

Contact Request

Close

We will call you right away. All information is kept private