let deepThought = {(ask question: String) in return "The answer to\"\(question)\" is \(7 * 6)!"} deepThought(ask: "how old are you")
Adding closures to an Array
let sum = {(a:Int, b:Int) -> Int in return a + b} let sumf = {(a:Float, b:Float) -> Float in return a + b} let closures = [sum, sumf]
func foo(x:Int) -> Int{ return 42 + x } let bar = {(x: Int) -> Int in 42 + x }
func curly(n:Int) -> Int{ return n * n } func larry(x: Int) -> Int{ return x * (x + 1) } func moe(m: Int) -> Int{ return m * (m - 1) * (m - 2) } var stooges = [curly, larry, moe] stooges.append(bar) for stooge in stooges{ stooge(42) } func baz(x:Int)->Double{ return Double(x) / 42 }
type Alias
//: Typealias typealias Integer = Int let z: Integer = 42 let zz: Int = 42 // (Int)->Int typealias IntToInt = (Int)->Int typealias IntMaker = (Void)->Int