Like OOP (Object-Oriented Programming), Functional Programming is a kind of programming paradigm. It is a programming style in which we write programs in terms of pure functions and immutable data. It treats its programs as function evaluation.
As we use pure functions and immutable data to write our applications, we will get lots of benefits for free. For instance, with immutable data, we do not need to worry about shared-mutable states, side effects, and thread-safety.
It follows a Declarative programming style, which means programming is done in terms of expressions, not statements. For instance, in OOP or imperative programming paradigms, we use statements to write programs where FP uses everything as expressions.
In this scala functional programming tutorial we will understand the principles and benefits of FP and why Functional reactive programming is a best fit for Reactive programming in Scala.
This Scala tutorial is an extract taken from the book Scala Reactive Programming written by Rambabu Posa.
FP has the following principles:
A pure function is a function that always returns the same results for the same inputs irrespective of how many times and where you run this function.
We will get lots of benefits with immutable data. For instance, no shared data, no side effects, thread safety for free, and so on.
Like an object is a first-class citizen in OOP, in FP, a function is a first-class citizen. This means that we can use a function as any of these:
In simple words, in FP, we treat both functions and data as the same.
We can compose functions that are in sequential order so that we can solve even complex problems easily. Higher-Order Functions (HOF) are functions that take one or more functions as their parameters or return a function as their result or do both.
For instance, map(), flatMap(), filter(), and so on are some of the important and frequently used higher-order functions. Consider the following example:
map(x => x*x)
Here, the map() function is an example of Higher-Order Function because it takes an anonymous function as its parameter. This anonymous function x => x *x is of type Int => Int, which takes an Int as input and returns Int as its result.
An anonymous function is a function without any name.
FP provides us with many benefits:
As we use pure functions and immutability in FP, we will get thread-safety for free.
One of the greatest benefits of FP is function composability. We can compose multiple functions one by one and execute them either sequentially or parentally. It gives us a great approach to solve complex problems easily.
The combination of FP and RP is known as function Reactive programming or, for short, FRP. It is a multiparadigm and combines the benefits and best features of two of the most popular programming paradigms, which are, FP and RP.
FRP is a new programming paradigm or a new style of programming that uses the RP paradigm to support asynchronous non-blocking data streaming with backpressure and also uses the FP paradigm to utilize its features (such as pure functions, immutability, no side effects, RT, and more) and its HOF or combinators (such as map, flatMap, filter, reduce, fold, and zip).
In simple words, FRP is a new programming paradigm to support RP using FP features and its building blocks.
FRP = FP + RP, as shown here:
Today, we have many FRP solutions, frameworks, tools, or technologies. Here's a list of a few FRP technologies:
This book is dedicated toward discussing Lightbend's FRP technology stack—Lagom Framework, Scala, Play Framework, and Akka Toolkit (Akka Streams).
FRP technologies are mainly useful in developing interactive programs, such as rich GUI (graphical user interfaces), animations, multiplayer games, computer music, or robot controllers.
Even though most of the projects or companies use FP Paradigm to develop their Reactive systems or solutions, there are a couple of ways to use RP. They are known as types of RP:
However, FP is the best programming paradigm to conflate with RP. We will get all the benefits of FP for free.
When we conflate RP with FP, we will get the following benefits:
In FP, we concentrate on what to do to fulfill a job, whereas in other programming paradigms, such as OOP or imperative programming (IP), we concentrate on how to do.
Declarative programming gives us the following benefits:
The main property of RP is real-time data streaming, and the main property of FP is composability. If we combine these two paradigms, we will get more benefits and can develop better solutions easily.
In RP, everything is a stream, while everything is a function in FP. We can use these functions to perform operations on data streams.
We learnt the principles and benefits of Scala functional programming. To build fault-tolerant, robust, and distributed applications in Scala, grab the book Scala Reactive Programming today.
Introduction to the Functional Programming
Manipulating functions in functional programming
Why functional programming in Python matters: Interview with best selling author, Steven Lott