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

Be Proactive: Use Reactive

Be Proactive: Use Reactive

What is “functional reactive programming?” Why should you use it? This talk is a high-level, easy-to-grasp intro to the core concepts behind reactive programming.

A video of this talk is available: https://vimeo.com/125191686

And speakers' notes, etc. are on my blog: https://www.dzombak.com/talks/2015-04-be-proactive-use-reactive.html

Chris Dzombak

April 16, 2015
Tweet

More Decks by Chris Dzombak

Other Decks in Programming

Transcript

  1. apiClient.performRequest(request) .subscribeNext({ (response) -> Void in // do something with

    response }, error: { (error) -> Void in println(“Error: \(error)”) }, completed: { () -> Void in println(“Done.”) })
  2. 1 3 2 2 6 4 map { (x) ->

    Int in x * 2 } Streams are composable
  3. 1 2 4 filter { (x) -> Bool in x

    % 2 == 0 } 4 2 Streams are composable
  4. taps .bufferWithTime(0.5) .map { ($0 as RACTuple).count } .filter {

    ($0 as Int) == 2 } .subscribeNext { (_) in println("Double tap!") }
  5. taps .bufferWithTime(0.5) .map { ($0 as RACTuple).count } .filter {

    ($0 as Int) == 2 } .subscribeNext { (_) in println("Double tap!") }
  6. taps .bufferWithTime(0.5) .map { ($0 as RACTuple).count } .filter {

    ($0 as Int) == 2 } .subscribeNext { (_) in println("Double tap!") }
  7. 1 2 3 taps .bufferWithTime(0.5) .map { ($0 as RACTuple).count

    } .filter { ($0 as Int) == 2 } .subscribeNext { (_) in println("Double tap!") }
  8. 2 taps .bufferWithTime(0.5) .map { ($0 as RACTuple).count } .filter

    { ($0 as Int) == 2 } .subscribeNext { (_) in println("Double tap!") }
  9. taps .bufferWithTime(0.5) .map { ($0 as RACTuple).count } .filter {

    ($0 as Int) == 2 } .subscribeNext { (_) in println("Double tap!") } Double tap!
  10. RACSignal.combineLatest([ slowToEmitStream, evenSlowerToEmitStream, ]) .doNext { let tuple = $0

    as RACTuple processResults( tuple.first, tuple.second) } .deliverOn(RACScheduler.mainThreadScheduler()) .subscribeCompleted { () -> Void in println("The work is done!") }
  11. let input = [1, 2, 3] var output = [Int]()

    for number in input { output.append(number * 2) } // output: [2, 4, 6] let output = [1, 2, 3].map { $0 * 2 } // output: [2, 4, 6]
  12. let input = [1, 2, 3] var output = [Int]()

    for number in input { output.append(number * 2) } // output: [2, 4, 6] let output = [1, 2, 3].map { $0 * 2 } // output: [2, 4, 6] Concept: 1-1 mapping
  13. var count = 0 upButton.addTarget(self, action: “upButtonTouched:”, forControlEvents: .TouchUpInside) downButton.addTarget(self,

    action: “downButtonTouched:", forControlEvents: .TouchUpInside) countLabel.text = String(count) func upButtonTouched(sender: UIButton) { count++ countLabel.text = String(count) } func downButtonTouched(sender: UIButton) { count-- countLabel.text = String(count) }
  14. let upTaps = upButton .rac_signalForControlEvents(.TouchUpInside) let downTaps = downButton .rac_signalForControlEvents(.TouchUpInside)

    let count = RACSignal.merge([ RACSignal.return(0), upTaps.mapReplace(1), downTaps.mapReplace(-1) ]) .scanWithStart(0, reduce: { $0 + $1 }) .map { String($0) } count ~> RAC(self, "countLabel.text")
  15. client.loginWithSuccess({ client.loadCachedTweetsWithSuccess({ (tweets) in client.fetchTweetsAfterTweet(tweets.last, success: { (tweets) -> Void

    in // Now we can show our tweets }, failure: { (error) -> Void in presentError(error) }) }, failure: { (error) -> Void in presentError(error) }) }, failure: { (error) -> Void in presentError(error) })
  16. client.login() .then { return client.loadCachedTweets() } .flattenMap { (tweets) ->

    RACStream in return client.fetchTweetsAfterTweet(tweets.last) } .subscribeError({ (error) -> Void in presentError(error) }, completed: { () -> Void in // Now we can show our tweets })
  17. /// Clients should observe via property observer: public var title:

    String public let title: Signal<String, NoError>
  18. func loginWithSuccess( success: () -> Void, failure: (NSError) -> Void)

    { // function doesn’t return anything // it jumps into two callbacks based on return } func login() -> RACSignal { // function has a native return value // a data stream object that we can observe }
  19. Scheduler 
 schedules when and where work is performed
 


    deliverOn(Scheduler) subscribeOn(Scheduler) reactive concurrency
  20. Missing RX Intro Input & Output RxMarbles Other j.mp/missingrxintro j.mp/joshaberio

    rxmarbles.com pinboard.in/u:cdzombak/t:reactiveprogramming