1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| let someClosure: (String, String) -> Bool = { (s1: String, s2: String) -> Bool in let isAscending: Bool
if s1 > s2 { isAscending = true } else { isAscending = false }
return isAscending }
let optimizedSomeClosure: (String, String) -> Bool = { $0 > $1 }
someClosure("apple", "banana")
optimizedSomeClosure("apple", "banana")
someClosure("banana", "apple")
optimizedSomeClosure("banana", "apple")
let otherClosure: ([Int]) -> Int = { (values: [Int]) -> Int in var count: Int = 0 for _ in values { count += 1 } return count }
let optimizedOtherClosure: ([Int]) -> Int = { $0.count }
otherClosure([0, 1, 2, 3, 4]) optimizedOtherClosure([0, 1, 2, 3, 4])
otherClosure([0]) optimizedOtherClosure([0])
|