Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Scala Reactive Programming

You're reading from   Scala Reactive Programming Build scalable, functional reactive microservices with Akka, Play, and Lagom

Arrow left icon
Product type Paperback
Published in Feb 2018
Publisher Packt
ISBN-13 9781787288645
Length 552 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Rambabu Posa Rambabu Posa
Author Profile Icon Rambabu Posa
Rambabu Posa
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Getting Started with Reactive and Functional Programming FREE CHAPTER 2. Functional Scala 3. Asynchronous Programming with Scala 4. Building Reactive Applications with Akka 5. Adding Reactiveness with RxScala 6. Extending Applications with Play 7. Working with Reactive Streams 8. Integrating Akka Streams to Play Application 9. Reactive Microservices with Lagom 10. Testing Reactive Microservices 11. Managing Microservices in ConductR 12. Reactive Design Patterns and Best Practices 13. Scala Plugin for IntelliJ IDEA 14. Installing Robomongo 15. Other Books You May Enjoy

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.

You have been reading a chapter from
Scala Reactive Programming
Published in: Feb 2018
Publisher: Packt
ISBN-13: 9781787288645
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image