Skip to Content
Scala Reactive Programming
book

Scala Reactive Programming

by Rambabu Posa
February 2018
Intermediate to advanced
552 pages
13h 46m
English
Packt Publishing
Content preview from Scala Reactive Programming

Advantages of flatMap

We can get the following benefits from the Scala flatMap() function:

  • It avoids if...else blocks
  • No nested for loops
  • No callback hell
  • We can write simple, elegant, readable, and neat code

The flatMap() function is defined, as shown here in all Scala Container classes:

final def flatMap[B](f: (A) ⇒ C[B]): C[B] 

Here, C is a Scala container class like List, Option, Try, Future, and so on.

The flatMap() function takes a function f as an argument. The function f is defined as f: A => C[B], meaning it takes element A as input, processes it, and outputs it as C[B].

Here, A and B are of the same type or may be different types. The following diagram shows how the Scala flatMap() function works internally:

Let us explore the ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Scala Programming Projects

Scala Programming Projects

Mikael Valot, Nicolas Jorand
Learning Scala

Learning Scala

Jason Swartz
Functional Programming in Scala

Functional Programming in Scala

Runar Bjarnason, Paul Chiusano

Publisher Resources

ISBN: 9781787288645Supplemental Content