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

Five Unbelievable Secrets of Reactive Programming the Experts Don't Want You to Know!

Greg Heo
February 24, 2017

Five Unbelievable Secrets of Reactive Programming the Experts Don't Want You to Know!

Totally non-clickbaity article about lessons learned from reactive programming. Presented at Playgrounds, Melbourne, February 24, 2917.

Greg Heo

February 24, 2017
Tweet

More Decks by Greg Heo

Other Decks in Technology

Transcript

  1. FIVE ✋
    Of Reactive Programming
    Unbelievable! Secrets
    The Experts Don’t Want
    YOU To Know!

    View full-size slide

  2. $
    You already know it!

    View full-size slide

  3. Data flows
    Propagation of change

    View full-size slide

  4. '
    Sequences!

    View full-size slide

  5. A type that provides sequential,
    iterated access to its elements.
    Sequence

    View full-size slide

  6. A type that provides sequential,
    iterated access to its elements.
    Sequence

    View full-size slide

  7. A type that provides sequential,
    iterated access to its elements.
    Sequence

    View full-size slide

  8. Sequence
    indexable
    single direction
    unbounded?

    View full-size slide

  9. View

    controller
    (x, y)
    Game

    controller
    Square
    View
    Move
    index

    View full-size slide

  10. SEQUENCES
    SEQUENCES EVERYWHERE

    View full-size slide

  11. )
    Small pieces of logic

    View full-size slide

  12. View

    controller
    (x, y)

    View full-size slide

  13. .map({ (point: NSPoint) -> Int in
    for (i, squareView) in squareViews.enumerated() {
    if squareView.frame.contains(point) {
    return i
    }
    }
    })
    vc.rx_mouseClicks an observable (aka a sequence)
    0 1 2
    3 4 5
    6 7 8

    View full-size slide

  14. View

    controller
    (x, y) Game

    controller
    Square
    index
    Square
    index

    View full-size slide

  15. Game

    controller
    View

    controller
    (x, y) Square
    index

    View full-size slide

  16. *
    Declarative style

    View full-size slide

  17. View

    controller
    (x, y)
    Game

    controller
    Square
    View

    controller
    Result
    index
    View
    Move

    View full-size slide

  18. gameController.resultObservable
    .subscribe(onNext: { moveResult in
    switch moveResult {
    case .win(let winner):
    let alert = NSAlert()

    // Show alert for winner
    case .draw:
    let alert = NSAlert()

    // Show alert for draw
    case .success(nextPlayer: player):

    // ...
    case .invalid(nextPlayer: player):
    // ...
    }
    })

    View full-size slide

  19. gameController.moveObservable
    .subscribe(onNext: { singleMove in
    squareViews[singleMove.square].squareState = singleMove.player
    })
    0 1 2
    3 4 5
    6 7 8

    View full-size slide

  20. View

    controller
    (x, y)
    Game

    controller
    Square
    View

    controller
    Result
    index
    View
    Move

    View full-size slide

  21. +
    Testable code

    View full-size slide

  22. Game

    controller
    Square index
    (Int)
    Result

    View full-size slide

  23. Game

    controller
    let moves: [Int] = [1, 3, 52, 4]
    [.success(nextPlayer: .nought),
    .success(nextPlayer: .cross),
    .success(nextPlayer: .nought)]
    .invalid(nextPlayer: .cross),

    View full-size slide

  24. [.success(nextPlayer: .nought),
    .success(nextPlayer: .cross),
    .success(nextPlayer: .nought)]
    .invalid(nextPlayer: .cross),
    XCTAssertEqual(
    , expected)

    View full-size slide

  25. Data flows
    Propagation of change

    View full-size slide

  26. 1. You already know it
    2. Sequences everywhere
    3. Small pieces of logic
    4. Declarative style
    5. Testable code

    View full-size slide

  27. so reactive
    very sequence
    much declarative
    such testable wow

    View full-size slide

  28. so reactive
    very sequence
    much declarative
    such testable
    wow

    View full-size slide

  29. feedback

    gregheo.com

    View full-size slide