Book Image

Scala Reactive Programming

By : Rambabu Posa
Book Image

Scala Reactive Programming

By: Rambabu Posa

Overview of this book

Reactive programming is a scalable, fast way to build applications, and one that helps us write code that is concise, clear, and readable. It can be used for many purposes such as GUIs, robotics, music, and others, and is central to many concurrent systems. This book will be your guide to getting started with Reactive programming in Scala. You will begin with the fundamental concepts of Reactive programming and gradually move on to working with asynchronous data streams. You will then start building an application using Akka Actors and extend it using the Play framework. You will also learn about reactive stream specifications, event sourcing techniques, and different methods to integrate Akka Streams into the Play Framework. This book will also take you one step forward by showing you the advantages of the Lagom framework while working with reactive microservices. You will also learn to scale applications using multi-node clusters and test, secure, and deploy your microservices to the cloud. By the end of the book, you will have gained the knowledge to build robust and distributed systems with Scala and Akka.
Table of Contents (16 chapters)

Functional programming

So far, we have discussed RP. Now it's time to move to FP (Functional Programming). Before discussing FRP, we should understand what FP is. We will discuss what FP is, its principles, and its benefits in this section.

What is functional programming?

Like OOP (Object-Oriented Programming), FP 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.

Principles of functional programming

FP has the following principles:

  • Pure functions
  • Immutable data
  • No side effects
  • Referential transparency (RT)
  • Functions are first-class citizens
  • Functions that include anonymous functions, higher order functions, combinators, partial functions, partially-applied functions, function currying, closures
  • Tail recursion
  • Functions composability

We will discuss these principles or properties of FP in brief here because we have a dedicated chapter on these concepts. Refer to Chapter 2, Functional Scala, to understand these concepts in-depth with some simple examples.

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:

  • An object
  • A value
  • A data
  • A data type
  • An operation

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.

Refer to Chapter 2, Functional Scala, to understand these concepts very well. I have provided a useful description and also some simple and easy-to-understand examples.

Benefits of functional programming

FP provides us with many benefits:

  • Thread-safe code
  • Easy-to-write concurrency and parallel code
  • We can write simple, readable, and elegant code
  • Type safety
  • Composability
  • Supports Declarative programming

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.

Functional Reactive programming

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).

Refer to Chapter 7, Working with Reactive Streams, to know more about backpressure.

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:

  • Scala, Play Framework, and Akka Toolkit
  • RxJS
  • Reactive-banana
  • Reactive
  • Sodium
  • Haskell

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.

Types of RP

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:

  • FRP (Functional Reactive Programming)
  • OORP (Object-Oriented Reactive Programming)

However, FP is the best programming paradigm to conflate with RP. We will get all the benefits of FP for free.

Why FP is the best fit for RP

When we conflate RP with FP, we will get the following benefits:

  • Composability—we can compose multiple data streams using functional operations so that we can solve even complex problems easily
  • Thread safety
  • Readability
  • Simple, concise, clear, and easy-to-understand code
  • Easy-to-write asynchronous, concurrent, and parallel code
  • Supports very flexible and easy-to-use operations
  • Supports Declarative programming
  • Easy to write, more Scalable, highly available, and robust code

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:

  • No side effects
  • Enforces to use immutability
  • Easy to write concise and understandable code

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.