- Swift Functional Programming(Second Edition)
- Dr. Fatih Nayebi
- 60字
- 2021-07-02 23:54:27
The map function
The map function is a higher-order function that solves the problem of transforming the elements of an array using a function. Consider the following example:
let numbers = [10, 30, 91, 50, 100, 39, 74]
var formattedNumbers: [String] = []
for number in numbers {
let formattedNumber = "\(number)$"
formattedNumbers.append(formattedNumber)
}
let mappedNumbers = numbers.map{ "\($0)$" }