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)

Scala Future.sequence()

When we have a sequence of Futures, or a list of Futures, or a collection of Futures, we should use one of the Future combinator APIs, Future.sequence(), to deal with this scenario.

This Future.sequence() function converts a list of Futures into a single Future that means collections of Futures into a single Future.

In simple words, List[Future[T]] ======> Future[List[T]].

It is also known as composing Futures. The following diagram demonstrates how Scala Future's sequence() function converts List[Future[T]] into Future[List[T]]:

Consider the following example:

scala> val pricesList:List[Future[Int]] = List(Future(1001),Future(999),Future(-2000),Future(1000)) 
pricesList: List[scala.concurrent.Future[Int]] = List(scala.concurrent.impl.Promise$DefaultPromise@680a66dd, scala.concurrent.impl.Promise$DefaultPromise@2dd8239, scala.concurrent.impl...