Chapter 10. Understanding map, flatMap, and compactMap

This chapter covers

  • Mapping over arrays, dictionaries, and other collections
  • When and how to map over optionals
  • How and why to flatMap over collections
  • Using flatMap on optionals
  • Chaining and short-circuiting computations
  • How to mix map and flatMap for advanced transformations

Modern languages like Swift borrow many concepts from the functional programming world, and map and flatMap are powerful examples of that. Sooner or later in your Swift career, you’ll run into (or write) code that applies map and flatMap operations on arrays, dictionaries, and even optionals. With map and flatMap, you can write hard-hitting, succinct code that is immutable—and therefore safer. Moreover, since ...

Get Swift in Depth now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.