How to apply a callback to an array (apply a function to each element) in Swift

1 Answer

0 votes
// Using map

import Foundation

func double(_ x: Int) -> Int {
    return x * 2
}

let numbers = [5, 10, 15, 20]

// Apply the callback to each element
let doubled = numbers.map(double)

print(doubled)   



/*
run:

[10, 20, 30, 40]

*/

 



answered Mar 21 by avibootz
...