In Swift `map` applies a transform closure to each element and returns a collection of results. `compactMap` applies a transform returning an optional and removes any nils from the result, producing a non-optional collection of non-nil results. `flatMap` is used when the transform returns a sequence (or optional) and you want to flatten nested collections (or flatten optionals plus sequences). /n/n For example: `let nums = ["1","2","a","3"]; let ints1 = nums.map { Int($0) }` gives `[Optional(1), Optional(2), nil, Optional(3)]`; `let ints2 = nums.compactMap { Int($0) }` gives `[1,2,3]`; `let groups = [[1,2],[3,4]]; let flat = groups.flatMap { $0 }` gives `[1,2,3,4]`. /n/n Understanding when to use each is often asked in Swift interviews.