Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Al Skipp | The Monad Among Us

Al Skipp | The Monad Among Us

Presented at www.swiftsummit.com

Swift Summit

March 21, 2015
Tweet

More Decks by Swift Summit

Other Decks in Technology

Transcript

  1. Name that nil in one (A wild guess — is

    it an Optional?) enum Optional<T> { case None case Some(T) }
  2. Name that nil in one (a gentle start) let x:Bool?

    = nil let x = Optional<Bool>.None
  3. map(nil) { $0 + "!" } Name that nil in

    one (Type inference required) ???
  4. Name that nil in one (Type inference required) map(nil) {

    $0 + "!" } map(Optional<String>.None) { $0 + "!" }
  5. protocol NilLiteralConvertible { init(nilLiteral: ()) } enum Optional<T> : NilLiteralConvertible

    { init(nilLiteral: ()) { self = None } } How to get an Optional from ‘nil’
  6. nil is a phantom The advantage of Optionals is not

    what they add to the language, but what they enable to be removed: nil
  7. Escape from the land of the nils In many languages

    nil roams free and you must take precautions to remain safe
  8. let dict = [1:[2:[3:[4:[5:"Hello"]]]]] let v = dict[1][2][3][4][5] A language

    with nil You've gotta ask yourself one question: “Do I feel lucky?”
  9. Well, do ya, punk? … (Yep) let dict = [1:[2:[3:[4:[5:"Hello"]]]]]

    let v = dict[1][2][3][4][5] Possible results: 1. v == "Hello"
  10. let dict = [1:[2:[3:[4:[5:"Hello"]]]]] let v = dict[1][2][3][4][0] Possible results:

    1. v == "Hello" 2. v == nil Well, do ya, punk? … (Not sure?)
  11. let dict = [1:[2:[3:[4:[5:"Hello"]]]]] let v = dict[1][0][3][4][5] Possible results:

    1. v == "Hello" 2. v == nil 3. Boom!!! Well, do ya, punk? … (Argh!)
  12. Swift style (Houston, we've got a problem) let dict =

    [1:[2:[3:[4:[5:"Hello"]]]]] let a:String? ! switch dict[1] { case .None : a = .None case .Some(let d) : switch d[2] { case .None : a = .None case .Some(let d) : switch d[3] { case .None : a = .None case .Some(let d) : switch d[4] { case .None : a = .None case .Some(let d) : switch d[5] { case .None : a = .None case .Some(let x) : a = x } } } } }
  13. Swift style (safe, yet insane) let a:String? ! switch dict[1]

    { case .None : a = .None case .Some(let d) : switch d[2] { case .None : a = .None case .Some(let d) : switch d[3] { case .None : a = .None case .Some(let d) : switch d[4] { case .None : a = .None case .Some(let d) : switch d[5] { case .None : a = .None case .Some(let x) : a = x } } } } }
  14. func >>= <A,B>(x: A?, f: A -> B?) -> B?

    Monadic bind for Optionals
  15. func >>= <A,B>(x: A?, f: A -> B?) -> B?

    Monadic bind for Optionals
  16. func >>= <A,B>(x: A?, f: A -> B?) -> B?

    Monadic bind for Optionals Optional Not Optional
  17. func >>= <A,B>(x: A?, f: A -> B?) -> B?

    { switch x { case .None : return .None case .Some(let x) : return f(x) } } Boilerplate code is abstracted away
  18. The monadic nature of Optional chaining let v = dict[1]?[2]?[3]?[4]?[5]

    let v = dict[1] >>= { $0[2] } >>= { $0[3] } >>= { $0[4] } >>= { $0[5] }
  19. let v = dict[1]?[2]?[3]?[4]?[5] let v:String? ! if let a

    = dict[1], b = a[2], c = b[3], d = c[4], e = d[5] { v = e } else { v = .None } Two monadic binds are better than one
  20. let v = dict[1]?[2]?[3]?[4]?[5] let v = dict[1].flatMap { $0[2]

    } .flatMap { $0[3] } .flatMap { $0[4] } .flatMap { $0[5] } Three monadic binds are better than one
  21. Conclusion • The nil is dead. Long live the Optional.

    • Code without nil is saner code.
  22. Conclusion • The nil is dead. Long live the Optional.

    • Code without nil is saner code. • Optionals are simple values, but do add complexity.
  23. Conclusion • The nil is dead. Long live the Optional.

    • Code without nil is saner code. • Optionals are simple values, but do add complexity. • Monadic bind is a natural way to tame the complexity.
  24. Conclusion • The nil is dead. Long live the Optional.

    • Code without nil is saner code. • Optionals are simple values, but do add complexity. • Monadic bind is a natural way to tame the complexity. • Swift deals with Optionals monadically, so do you.