map vs compactMap

What are the differences?

Photo by Annie Spratt on Unsplash

map(_:)

Returns an array containing the results of mapping the given closure over the sequence’s elements.
Apple Documentation

compactMap(_:)

Returns an array containing the non-nil results of calling the given transformation with each element of this sequence.
Apple Documentation

Sources:

Chuck Norris can hear sign language

--

--

iOS Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store