I have a function evaluate
that takes arguments. The first argument is an Int
. The second argument of a closure that takes an Int
and returns a Double
. The function evaluate
then returns a [Double]
. The kβth element of the returned array is the result of evaluating the second argument with the value k for k = 0, 1, ..., n.
func evaluate(n: Int, myFunction: Int -> Double) -> [Double] {
var doubles = [Double]()
for i in 1...n {
doubles[i] = myFunction(i)
}
return doubles
}
let polyTableClosure: Int -> Double = { return Double($0 * $0 * $0 + 2 * $0 + 4) }
print(evaluate(5, polyTableClosure))
Expecting something like: [7, 16, 37, 76, 139]