Gieta Laksmana
  • Blog
  • Profile
  • Projects
    • VR World Arcade
    • Punch For Fun
    • Spectrum
    • Dimension Battle
  • Resume

Interesting Swift Lambda (closure) syntax

6/23/2016

0 Comments

 
Learning swift for iOS and macOS development. I was creating a closure to sort strings reversely this way:
let reversed = names.sorted(by: { 
    (s1: String, s2: String) -> Bool in 
    return s1 > s2 
})
The sort() function simply takes a function that takes 2 strings and returns a bool. What I didn't realize is the the "operator >" that I'm using on the closure is itself a function that takes 2 strings and return a bool.

So that chunk above can be reduced to:
let reversed = names.sorted(by: >)
0 Comments



Leave a Reply.

    Archives

    September 2018
    August 2018
    July 2018
    September 2017
    July 2017
    November 2016
    October 2016
    September 2016
    June 2016
    May 2016
    March 2016

    Categories

    All
    Shenanigans
    Software Engineering
    Swift
    Vr
    Xcode

    RSS Feed

Powered by Create your own unique website with customizable templates.