© Toby Weston 2018
Toby WestonScala for Java Developershttps://doi.org/10.1007/978-1-4842-3108-1_17

17. Map and FlatMap

Toby Weston
(1)
London, UK
 
In this chapter, we’ll look at some of the functional programming features of Scala, specifically the ubiquitous map and flatMap functions. We’re interested in these because they’re closely related to the idea of monads, a key feature of functional programming.

Mapping Functions

You’ll see the map function on countless classes in Scala. It’s often described in the context of collections. Classes like List, Set, and Map all have it. For these, it applies a given function to each element in the collection, giving back a new collection based on the result of that function. You “map” some function over each ...

Get Scala for Java Developers: A Practical Primer 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.