This is a Swift 2.0 article, get this and other playgrounds from GitHub.
Swift is a language still slightly in flux, with new functionalities and alterations of behavior being introduced in every release. Much has already been written about the functional aspects of Swift and how to approach problems following a more “pure” functional approach.
Considering that the language is still in its infancy, often, trying to understand some specific topics you’ll end up reading a lot of articles referring to old releases of the language, or worst, descriptions that mix up different releases. Sometimes, searching for articles on
flatMap, you could even fortuitously find more than one really good articles explaining Monads in the context of Swift.
Add to the lack of comprehensive and recent material the fact that many of these concepts, even with examples or daring metaphors, are not obvious, especially for someone used to the imperative way of thinking.
With this short article I’ll try to give a clear and throughout explanation of how
map and especially
flatMap work for different types in Swift 2.0, with references to the current library headers.