map and flatMap are mainly used on sets and on optional types. Let's take a look at them respectively.
Use map and flatMap on collections
Let's look at the following code first:
func getInfos(by name: String) -> [String] { if name == "Jack" { return ["Male", "25", "New York"] } else if name == "Lucy" { return ["Female", "18", "London"] } else { return ["Unkown", "Unkown", "Unkown"] } } let names = ["Jack", "Lucy", "Nobody"] let infos1 = { getInfos(by: $0) } print(infos1) let infos2 = { getInfos(by: $0) } print(infos2)
The input is a one-dimensional array. The result of infos1 after conversion is the following two-dimensional array, so there are two-layer structures after map:
[["Male", "25", "New York"], ["Female", "18", "London"], ["Unkown", "Unkown", "Unkown"]]
The input is a one-dimensional array. The result of infos2 after conversion is the following one-dimensional array, so there is only one-layer structure after flatMap:
["Male", "25", "New York", "Female", "18", "London", "Unkown", "Unkown", "Unkown"]
The implementation of map on Array is roughly as follows:
extension Array { func map<T>(_ transform: (Element) -> T) -> [T] { var result: [T] = [] for x in self { (transform(x)) } return result } }
The implementation of flatMap on Array is roughly as follows:
extension Array { func flatMap<T>(_ transform: (Element) -> [T]) -> [T] { var result: [T] = [] for x in self { (contentsOf: transform(x)) } return result } }
Use map and flatMap on optional types
In the following code, the input is , its type is String?
- The type of x after conversion is Int??, so there are two layers of Optional after map
- The type of y after conversion is Int?, so there is only one layer after flatMap Optional
let stringNumbers = ["1", "2", "3", "foo"] let x = { Int($0) } // Optional(Optional(1)) let y = { Int($0) } // Optional(1)
The implementation of map on Optional is roughly as follows:
extension Optional { func map<U>(transform: (Wrapped) -> U) -> U? { if let value = self { return transform(value) } return nil } }
The implementation of flatMap on Optional is roughly as follows:
extension Optional { func flatMap<U>(transform: (Wrapped) -> U?) -> U? { if let value = self, let transformed = transform(value) { return transformed } return nil } }
The above is the Swift Advanced - Detailed explanation of map and flatMap. For more information about Swift map and flatMap, please follow my other related articles!