In this new series on Swift code topics, descriptions of various basic features of the Swift programming language will be explored with examples of code. I use Xcode playgrounds for most of my prototype code development since that platform fosters exploration. The code examples used in this post have been uploaded to GitHub.

Since its introduction to the Apple development platform in 2014 (announced at WWDC 2014), Swift continues to mature rapidly due to its open development community (Swift.org). As such, Swift code is cleaner to write, is strongly typed, compiles to a fast executable, and includes many safe design principles.

In this post, the methods (map, flatMap, compactMap, and filter) are examined.

Method: map(_:)

The map(_:) method applies a function on each item in a collection, essentially mapping one set of values to another set of values of possibly a different type. The map method is defined for a collection type (e.g., Dictionary, Set, Array), not just Arrays.

The first example squares the value of each integer value:

let numberArray = [1, 2, 3, 4, 5]
let squaredArray = numberArray.map { $0 * $0 }
print(squaredArray) // [1, 4, 9, 16, 25]

The second example converts an array of Fahrenheit temperatures to an array of Celsius temperatures:

let fahrenheit = [-40.0, -4.0, 32.0, 98.6, 212.0]
let celsius = fahrenheit.map { ($0 - 32) * 5 / 9 }
print(celsius) // [-40.0, -20.0, 0.0, 37.0, 100.0]

The third example converts the array of numbers to an array of strings:

let numbers = [1, 2, 3, 4, 5]
let strings = numbers.map { "\($0)" }
print(strings) // ["1", "2", "3", "4", "5"]

Method: flatMap(_:)

The flatMap(_:) method flattens a collection of collections into a collection.

An array of arrays is flatten to an array in the following example:

let arrayOfArrays = [ [1, 2], [3, 4]]
let flatArray = arrayOfArrays.flatMap { $0 }
print(flatArray) // [1, 2, 3, 4]

Method: compactMap(_:)

The compactMap(_:) method provides a convenient way to strip nil values from a collection (e.g., array).

The nil value result from an Int() conversion is stripped out of the array in the following example:

let numberStrings = ["77", "22", "notANumber"]
let intsOnly = numberStrings.compactMap { Int($0) }
print(intsOnly) // [77, 22]

Method: filter(_:)

The filter(_:) method applies a condition to the values of a collection type. Only the values that satisfy the condition are included in the resultant collection type.

The following example extracts the values from an array that are modulo 2:

let numbersArray = [10, 11, 12, 13, 14, 15]
let mod2Array = numbersArray.filter { $0 % 2 == 0 }
print(mod2Array) // [10, 12, 14]

Reference Information:

Map, FlatMap, and CompactMap by John Sundell.

Transforming collections in Swift by John Sundell.

Inside the Standard Library: Sequence.map() by Paul Hudson.

How To: Map, Reduce, and Filter in Swift by Reinder de Vries.

Swift Guide to Map Filter Reduce by Keith Harrison.

Swift – Map, FlatMap, Filter, and Reduce by Santosh Botre.

Credits

The Xcode logo is a copyright and trademark of Apple.
The CocoaPods logo is a copyright and trademark of CocoaPods.