Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
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
€8.99 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

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 : 9781787282872
Vendor :
Lightbend
Category :
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Feb 28, 2018
Length: 552 pages
Edition : 1st
Language : English
ISBN-13 : 9781787282872
Vendor :
Lightbend
Category :
Languages :
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
Banner background image

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

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.