Skip to content

Commit

Permalink
Updated the Closures chapter to the latest notation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan Antonio Karmy committed Oct 23, 2016
1 parent c853647 commit fd439c9
Showing 1 changed file with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@

var array = ["John", "Tim", "Steve"]

var reversed = array.sorted(isOrderedBefore: {(s1: String, s2: String) -> Bool in return s1 > s2})
var reversed = array.sorted(by: {(s1: String, s2: String) -> Bool in return s1 > s2})

//Using type inference, we can omit the params and return types. This is true when passing closures as params to a function.
reversed = array.sorted(isOrderedBefore: {s1, s2 in return s1 > s2})
reversed = array.sorted(by: {s1, s2 in return s1 > s2})

//In case of single-expression closures, the return value is implicit, thus the return expression can be omitted.
reversed = array.sorted(isOrderedBefore: {s1, s2 in s1 == s2})
reversed = array.sorted(by: {s1, s2 in s1 == s2})

//In the previous examples, the names of the closure's params were explicit. You can use the $X variables to refer to params for the closure.
//This eliminates the need for the first params list, which makes the body the only relevant part.
reversed = array.sorted(isOrderedBefore: {$0 == $1})
reversed = array.sorted(by: {$0 == $1})

//We can even take this to an extreme. String defines its own implementation for the ">" operator, which is really all the closure does.
reversed = array.sorted(isOrderedBefore: >)
reversed = array.sorted(by: >)

/*##### TRAILING CLOSURES #####*/
func someFunctionThatTakesAClosure(closure: () -> ()) {
Expand Down

0 comments on commit fd439c9

Please sign in to comment.