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
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Scala Reactive Programming
Scala Reactive Programming

Scala Reactive Programming: Build scalable, functional reactive microservices with Akka, Play, and Lagom

eBook
€20.98 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. €18.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Scala Reactive Programming

Functional Scala

In this chapter, we will discuss some of the important and useful FP (Functional Programming) principles of the Scala programming language. I assume that you are already familiar with Scala basics such as Scala REPL, Scala OOPs concepts, and more. If you are really new to the Scala world, it'll be helpful if you read some Scala basics first and come to us later.

In this chapter, you will learn the following topics:

  • Scala App
  • How to use Scala REPL
  • Scala FP features
  • Scala Functional Design Patterns
  • Scala plug-in for IntelliJ IDEA
  • The Scala Collections API
  • Trait and its linearization rules

Introduction to Scala

Scala stands for Scalable language. Scala is a multi-paradigm programming language built on JVM by the Lightbend (formerly known as Typesafe) team. In Scala, it is easy to write concurrent parallel, distributed, and Reactive applications in a concise, elegant, and type-safe way.

Unlike Java, Scala is a pure OOP and FP language. Java is not a pure OOP language because of the following:

  • It supports static members
  • It supports primitive data types

Scala does not support static members. Then how do we define utility methods in Scala? We will explore those in the upcoming sections. In Scala, everything is an object only. There are no primitive types in Scala.

Both Scala and Java programming languages run on JVM:

From Java 8 onward, we can write functional-style programming in Java. However, it does not support all FP features.

Scala's latest stable version...

Principles of Scala FP

Scala supports all FP features. However, it is not easy to cover them in a single chapter. So we will discuss only the following few important FP features in this chapter:

  • Pure functions
  • Immutability (Immutable data)
  • Referential transparency
  • Functions are first-class citizens
  • Anonymous functions
  • Higher-Order Functions (HOF)
  • Currying
  • Tail recursion
  • Implicit
  • Typeclasses

FP Design Patterns

The following three are the most important and frequently used Functional Design Patterns.

  • Monoid
  • Monad
  • Functor (Applicative functor)
If you are new to Scala FP features and want to learn them in depth, I recommend that you refer to any Scala Basics books before picking this one.

Consider the following Scala FP features...

Scala anonymous functions

In previous sections, we defined a couple of Scala functions and came to know that each function has a meaningful name. Is it possible to define a function without a name? Yes, in Scala, we can define a function without a name.

An anonymous function is a function without a name. It is also known as a function literal. It works just like a normal function. Let's explore it with some examples now:

def add (a: Int, b: Int) = a + b 

The following diagram shows the syntax of a Scala anonymous function:

As we discussed with the add() function in the previous section, it is a normal Scala function.

We can write the same function as an anonymous function, as shown here:

scala> (a: Int, b: Int) => a + b 
res0: (Int, Int) => Int = <function2> 

Here, we have created an anonymous function of type Int, Int) => Int. It clearly explains what...

Everything is an expression

Unlike Java, in Scala, everything is an expression. Yes, that's right. Then how about if...else expressions, For-comprehension (for loop), case statements, and more?

In Scala, we can use an if...else as a statement or expression, as shown here:

scala> val x = 10 
x: Int = 10 
 
scala> if (x % 2 == 0) "Even" else "Odd" 
res4: String = Even 
 
scala> val result = if (x % 2 == 0) "Even" else "Odd" 
result: String = Even 
 
scala> result 
res5: String = Even 

In the same way, we can assign anything to a variable, as everything is an expression.

Referential transparency

In Scala, referential transparency (RT) means that an expression or a function call may be replaced by its value, without changing the behavior of the application.

A value may be replaced by an expression or a function call without changing the behavior of the application.

We will explore these two points with some simple examples now.

Let's assume that we are using the y = x + 1 expression in our application:

scala> val x = 10 
x: Int = 10 
 
scala> val y = x + 1 
y: Int = 11 
 
scala> val y = 11 
y: Int = 11 

Here, when we replace an expression x+1 with its value 11, it does not change any behavior of the y in our application:

scala> val y = 11 
y: Int = 11 
 
scala> def addOne(a: Int) = a + 1 
addOne: (a: Int)Int 
 
scala> val y = addOne(x) 
y: Int = 11 

Here, the value of y is replaced by a function called addOne(). In this case...

Functions are first-class citizens

In Scala, functions are first-class citizens because we can do the following things with them:

  • We can use functions as values or like normal variables; we can replace a variable or value with a function
  • We can assign a function literal to a variable
  • We can pass one or more functions as another function's parameters
  • We can return a function from another function

We will discuss the last two points in the Scala Higher-Order Functions section. Let's discuss the first two points here.

We can use a function like a normal value or variable, as shown here:

scala> def doubleIt(x: Int) = x * x 
doubleIt: (x: Int)Int 
 
scala> def addOne(x: Int) = x + 1 
addOne: (x: Int)Int 
 
scala> val result = 10 + doubleIt(20) + addOne(49) 
result: Int = 460 

We can assign an anonymous function to a variable, as shown in the following code snippet...

Scala tail-recursion

Recursion is a technique defined as using a function or method that calls itself again and again, until it solves the problem.

The recursion technique helps us solve simple problems very easily and even makes it possible to solve complex problems. It is easy to reason about and needs a lot less code, as compared to the iterative approach.

What kind of problems can we solve using the recursion technique? Any problem that is defined in terms of itself.

Types of recursions

We can implement recursion in different ways. However, we are considering only the following two types:

  • Linear recursion
  • Tail-recursion

Tail-recursion is one form of recursion technique. We can say a recursive call is tail-recursive,...

Scala Type class

In Scala, a Type class defines some behavior in terms of some operations. If a new class (or Type) wants to join as a member of that Type class, it must provide implementation to all operations, which are defined in that Type class.

So in simple words, a Type class is a group of classes which provides implementation to a contract (or an interface).

The main goal of Type classes is to define a contract or interface to its types.

Unlike sub-type polymorphism, which uses OOP inheritance, Type classes follow ad hoc polymorphism, using composition and support DRY (Do NOT Repeat Yourself) and SRP (Single Responsibility Principle).

Like generics or Type parameters, Type classes are resolved at compile time. The following diagram shows the main goal of Scala Type class:

Types of polymorphism in brief:

  • Parametric polymorphism—using generics:
      trait Json...

Scala Collections in action

The Scala language supports a very rich Collection API. It supports two sets of Collections—one to support immutability (immutable Collections) and another to support mutability (mutable Collections).

It requires almost an entire book to explain each and every Collection API in depth. As we have only one subsection in this chapter for Collections, we will discuss only the most important and frequently used Collections here.

Scala List

In the Scala Collection API, List is a LIFO-based immutable Collection which represents an ordered linked list (where LIFO stands for Last In First Out). It is available as scala.collection.immutable.List.

It has two implementation case classes, scala.Nil and...

Scala Functional Design Patterns

As a Java or Scala-experienced developer, I guess you are already familiar with some of the OOP design patterns. You may be not aware of Scala Functional Design Patterns.

Scala source code or applications use the following Functional Design Patterns extensively:

  • Monoid
  • Functor
  • Monad

All these three terminologies come from Mathematics Category Theory (MAT). Let's delve into each of these, one by one in the following sections:

  • Monoid: In Scala, Monoid is a type class or data structure with the following two rules:
    • Associative rule:
              (A1 Op A2) Op A3 == A1 Op (A2 Op A3)
    • Identity rule: This rule states that, suppose we make a call to a function with two elements. This Identity rule states if that function returns a second element as is, without any change, then that first element is known as an Identity element:

Left...

Scala Traits in action

Scala Traits are somewhat similar to Java 8's interface, but they do more. We can use a Scala Trait as an interface (contract), abstract class, class, Mixin, and more.

In Scala, a Trait can contain abstract code, concrete code, or both. We can create a trait using the trait keyword, as shown here:

Trait Syntax: 
 
trait <Trait-Name> { 
 
  // Abstract members (Data and Functions) 
 
  // Non-Abstract (Concrete) members (Data and Functions) 
 
}  

Let's explore them, one by one, now.

Trait as an interface

We can define a Trait only with abstract members, as shown here:

rambabuposa@ram$ scala -cp joda-time-2.9.6.jar
Welcome to Scala 2.12.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_121...

Summary

In this chapter, we discussed some Scala Basics and how to use the Scala app to execute standalone applications. We also discussed what is Scala REPL and how to access it.

We discussed a couple of Scala Functional Programming concepts with some simple examples, and gave an introduction to the Scala Collection API.

We discussed Scala FP Design Patterns such as Monoid, Functor, and Monad, and finally discussed a couple of Scala Source Code Monads.

Finally, we explored one of the greatest features of Scala—Traits. Java does not have a similar programming construct. We also explored linearization rules with one simple example.

In the next chapter, we will discuss the Scala asynchronous programming API.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • - Understand and use the concepts of reactive programming to build distributed systems running on multiple nodes.
  • - Learn how reactive architecture reduces complexity throughout the development process.
  • - Get to grips with functional reactive programming and Reactive Microservices.

Description

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.

Who is this book for?

This book is for Scala developers who would like to build fault-tolerant, scalable distributed systems. No knowledge of Reactive programming is required.

What you will learn

  • Understand the fundamental principles of Reactive and Functional programming
  • Develop applications utilizing features of the Akka framework
  • Explore techniques to integrate Scala, Akka, and Play together
  • Learn about Reactive Streams with real-time use cases
  • Develop Reactive Web Applications with Play, Scala, Akka, and Akka Streams
  • Develop and deploy Reactive microservices using the Lagom framework and ConductR

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Feb 28, 2018
Length: 552 pages
Edition : 1st
Language : English
ISBN-13 : 9781787288645
Vendor :
Lightbend
Category :
Languages :
Concepts :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. €18.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Feb 28, 2018
Length: 552 pages
Edition : 1st
Language : English
ISBN-13 : 9781787288645
Vendor :
Lightbend
Category :
Languages :
Concepts :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 110.97
Learning Scala Programming
€36.99
Scala Reactive Programming
€36.99
Scala Design Patterns
€36.99
Total 110.97 Stars icon

Table of Contents

15 Chapters
Getting Started with Reactive and Functional Programming Chevron down icon Chevron up icon
Functional Scala Chevron down icon Chevron up icon
Asynchronous Programming with Scala Chevron down icon Chevron up icon
Building Reactive Applications with Akka Chevron down icon Chevron up icon
Adding Reactiveness with RxScala Chevron down icon Chevron up icon
Extending Applications with Play Chevron down icon Chevron up icon
Working with Reactive Streams Chevron down icon Chevron up icon
Integrating Akka Streams to Play Application Chevron down icon Chevron up icon
Reactive Microservices with Lagom Chevron down icon Chevron up icon
Testing Reactive Microservices Chevron down icon Chevron up icon
Managing Microservices in ConductR Chevron down icon Chevron up icon
Reactive Design Patterns and Best Practices Chevron down icon Chevron up icon
Scala Plugin for IntelliJ IDEA Chevron down icon Chevron up icon
Installing Robomongo Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.8
(10 Ratings)
5 star 40%
4 star 0%
3 star 10%
2 star 0%
1 star 50%
Filter icon Filter
Top Reviews

Filter reviews by




Kishore Apr 01, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is great book to start and get understanding of scala reactive programming, each chapter has a detailed explanation with nice diagrams and examples, the topics are well organized and moving slowly from fundamental concepts to building robust systems with Scala and Akka and deploying your microservices to cloud, the summary in each chapter is very organized and connecting to previous and next learning highlights, the book is very impressive and I would recommend to read once in order to explore and develop reactive web applications with Play, Scala, Akka, and Akka Streams
Amazon Verified review Amazon
LoyalAmazonCustomer Apr 22, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
very nicely written and articulated the concepts. Good start for readers who are interested in reactive programming in Scala and Akka, as well deploying microservices in cloud. One of the great books i have read in recent years and would highly recommend to others.
Amazon Verified review Amazon
Bhavs Apr 19, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is a very nice book. This book covered all the topics starting from reactive programming to in-depth knowledge of reactive programming.It seems, The author has tremendous experience in both software engineering and teaching.This covers building Reactive Applications with Akka. It's good book and worth reading.
Amazon Verified review Amazon
Amazon Customer Apr 09, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is very usefull for people who are desperate of working on Microservices
Amazon Verified review Amazon
Szilágyi Donát Jun 12, 2019
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
The topics are good and the first part about Scala is a really good summary about this huge programming language. What I missed is the more practical info about setting up the environment and configuring the frameworks. Some of them (especially Lagom) was very difficult to start based on the info in this book. Sometimes I lost among the version numbers of Scala, SBT and the frameworks. But the code examples are practical and easy to understand.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.