In fact, s.flatMap(transform) is equivalent to Array(s.map(transform).joined()).

compactMap vs flatMap

The general rule of thumb here to remind yourself for when writing code:

Used on a sequence and having a transformation returning an optional value, use compactMap. If not, either map or flatMap should give you the results you need.

The reason for naming it compactMap

Although some proposed to introduce filterMap as the method name, the Swift team decided to go for compactMap instead. Reading their motivations clears things up quite a lot and explains the functionality of the method as well.

“filterMap” is a name with some precedent in other programming languages, especially functional ones, but some people felt strongly that the name was misleading because, as a combined operation, it wasn’t quite a filter or a map. Of the alternatives, the one with the strongest support seemed to be “compactMap”, which builds on the precedent of “compact”, an operation from other languages (notably Ruby) which returns a copy of the input without nil values. Swift does not currently have such an operation, and in fact it is not currently possible to express it; nonetheless, the core team agrees that it would be a reasonable operation to add, and that “compactMap” is a good name for the operation.