23 Working with Option: map and flatMap

After reading this lesson, you will be able to

  • Transform an element contained in an Option using the map operation
  • Simplify a nested optional structure using flatten
  • Chain optional values together using flatMap

In the previous lesson, you discovered the type Option and how to pattern match on it. After working with optional types for some time, you will realize that some of its operations are particularly recurrent. The class Option offers you a set of higher order functions for them to be more productive, and you do not have to use pattern matching every time. This lesson will introduce you to some of the most common and useful predefined functions on Option. You’ll discover how to transform an optional ...

Get Get Programming with Scala now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.