Wesley de Groot's Blog
Difference between map, flatMap, compactMap

Back

In this post, we'll explore the differences between map, flatMap, and compactMap in Swift. These three methods are commonly used when working with collections, and understanding their distinctions is crucial for writing clean and efficient code.

What is map?

map is a higher-order function that transforms each element of a collection using a provided closure. It returns a new collection with the transformed elements, leaving the original collection unchanged. The closure passed to map takes an element of the collection as input and returns a transformed value.

Here's a simple example of using map to double each element of an array of integers:

let numbers = [1, 2, 3, 4, 5]

let doubledNumbers = numbers.map { $0 * 2 }

print(doubledNumbers) // Output: [2, 4, 6, 8, 10]

In this example, the closure { $0 * 2 } is applied to each element of the numbers array, resulting in a new array with each element doubled.

map() can also transform each element of a collection using a provided closure and return a new collection with the transformed elements.

let numbers = [1, 2, 3, 4, 5]
print(numbers) // Output: [1, 2, 3, 4, 5]

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

let maybeNumbers = strings.map { Int($0) } // [1, 2, 3, 4, 5]
print(maybeNumbers) // Output: [Optional(1), Optional(2), Optional(3), Optional(4), Optional(5)]

let unwrappedNumbers = maybeNumbers.compactMap { $0 }
print(unwrappedNumbers) // Output: [1, 2, 3, 4, 5]

What is flatMap?

flatMap is a higher-order function that transforms each element of a collection using a provided closure and then flattens the resulting collection of collections into a single collection. It is particularly useful when working with nested collections or when you want to remove nil values from a collection.

Here's an example of using flatMap to flatten an array of arrays:

let nestedNumbers = [[1, 2], [3, 4], [5, 6]]

let flattenedNumbers = nestedNumbers.flatMap { $0 }

print(flattenedNumbers) // Output: [1, 2, 3, 4, 5, 6]

In this example, the closure { $0 } is applied to each element of the nestedNumbers array, resulting in a new array with the nested arrays flattened into a single array.

What is compactMap?

compactMap is a higher-order function that transforms each element of a collection using a provided closure and then removes any nil values from the resulting collection. It is particularly useful when working with collections that may contain optional values.

Here's an example of using compactMap to remove nil values from an array of optional integers:

let optionalNumbers: [Int?] = [1, nil, 3, nil, 5]

let nonNilNumbers = optionalNumbers.compactMap { $0 }

print(nonNilNumbers) // Output: [1, 3, 5]

In this example, the closure { $0 } is applied to each element of the optionalNumbers array, resulting in a new array with the nil values removed.

Use Cases

  • Use map when you want to transform each element of a collection.
  • Use flatMap when you want to transform each element of a collection and flatten the resulting collection of collections.
  • Use compactMap when you want to transform each element of a collection and remove any nil values.

Conclusion

In summary, map, flatMap, and compactMap are powerful tools for working with collections in Swift. By understanding their differences and use cases, you can write cleaner and more efficient code when transforming and filtering collections.

Resources:

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/collectiontypes/#Iterating-Over-an-Array

Read more

Share


Share Mastodon Twitter LinkedIn Facebook
x-twitter mastodon github linkedin discord threads instagram whatsapp bluesky square-rss sitemap