Swift Array.map closure issue
Edit: Note: This language feature was removed in Swift 2.
A swift-er way than connor's answer (but along the same lines), is to use a curried function. From The Swift Programming Language->Language Reference->Declarations->Curried Functions and Methods:
A function declared this way is understood as a function whose return type is another function.
So you can simplify this:
func multiplier(factor: Double) -> (Int)->Double{ return { (currentNum: Int) -> Double in let result = Double(currentNum) * factor return result }}
to this:
func multiplier(factor: Double)(currentNum: Int) -> Double { return Double(currentNum) * factor}
and use it exactly the same way:
let numbersArray = [1, 2, 3, 4, 5]let multipliedArray = numbersArray.map(multiplier(3.5))
You can use a higher order function to produce a custom function that you can then use with the array's map function. Like this:
var numbersArray = [1, 2, 3, 4, 5]func multiplier(factor: Double) -> (Int)->Double{ return { (currentNum: Int) -> Double in let result = Double(currentNum) * factor return result }}var newArray = numbersArray.map(multiplier(2.5))