Home > AI > IOS > Foundation >

flatMap(_:)

Example (Apple Official)

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

let mapped = numbers.map { Array(repeating: $0, count: $0) }
print(mapped)
// [[1], [2, 2], [3, 3, 3], [4, 4, 4, 4]]

let flatMapped = numbers.flatMap { Array(repeating: $0, count: $0) }
print(flatMapped)
// [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]

Leave a Reply