Currying: Difference between revisions

(add swift)
Line 567:
20</lang>
=={{header|Swift}}==
<lang Swift>func addN(n:Int) -> ((x:Int) -> Int) { return x + n }
{{trans|JavaScript}}
 
<lang Swift>func addN(n:Int) -> ((Int) -> Int) {
var add2 = addN(2)
func curry(x:Int) -> Int {
println(add2) // (Function)
return x + n
println(add2(x:7)) // 9</lang>
}
There is a bug is Swift (as of 1.1) which forces the second parameter to always be labeled. To avoid it, you can return a closure (or nested function):
return curry
<lang Swift>func addN(n:Int)->Int->Int { return {$0 + n} }
}
 
var add2 = addN(2)
Anonymous user