Slide 1

Slide 1 text

BE PROACTIVE USE REACTIVE

Slide 2

Slide 2 text

Nutshell.com CodeOptional.com @andrewa2 github.com/andrewsardone (by Kevin Vitale) andrew sardone

Slide 3

Slide 3 text

What is reactive programming?

Slide 4

Slide 4 text

“ ” reactive |rēˈaktiv| … a programming paradigm oriented around data flows and the propagation of change

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

reactive programming data flows propagation of change =

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

we all respond to flowing data and react to changes Model View Controller

Slide 10

Slide 10 text

Cocoa’s KVO? NSNotificationCenter? Ember or Angular observers? Callbacks? Listeners? How is this different from… Programming?

Slide 11

Slide 11 text

Back to the basics

Slide 12

Slide 12 text

Subject Observer notify()

Slide 13

Slide 13 text

DATA STREAMS

Slide 14

Slide 14 text

Data type a value over time

Slide 15

Slide 15 text

•updated property values •mouse cursor position •typing characters into a text field •database records

Slide 16

Slide 16 text

Collection of emitted events ordered in time

Slide 17

Slide 17 text

with functions for observation

Slide 18

Slide 18 text

onNext( ) onComplete()

Slide 19

Slide 19 text

onNext( ) onNext( ) onError() x

Slide 20

Slide 20 text

[ "value”, "other value”, "another value” ] "a value", "other value", "another value"

Slide 21

Slide 21 text

textField.rac_textSignal() .subscribeNext({ (_) -> Void in // value is emitted }, error: { (error) -> Void in // an error occurs within the stream }, completed: { () -> Void in // signal completes successfully })

Slide 22

Slide 22 text

Stream.transform(f: (x) -> y) -> Stream Streams are composable

Slide 23

Slide 23 text

1 3 2 2 6 4 map { (x) -> Int in x * 2 } Streams are composable

Slide 24

Slide 24 text

1 2 4 filter { (x) -> Bool in x % 2 == 0 } 4 2 Streams are composable

Slide 25

Slide 25 text

Double tap! Demo

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

RACSignal.combineLatest([ slowToEmitStream, evenSlowerToEmitStream, ]) .doNext { let tuple = $0 as RACTuple processResults( tuple.first, tuple.second) } .subscribeCompleted { () -> Void in println("The work is done!") }

Slide 34

Slide 34 text

MODEL WITH STREAMS

Slide 35

Slide 35 text

MODEL WITH STREAMS BUT…WHY?

Slide 36

Slide 36 text

Claim code should communicate intent

Slide 37

Slide 37 text

Current tooling is insufficient

Slide 38

Slide 38 text

map extension Array { func map(transform: (T) -> U) -> [U] }

Slide 39

Slide 39 text

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]

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

map reifies 1-1 data transformation

Slide 42

Slide 42 text

Good Thing™ reify |ˈrēəәˌfī| make (something abstract) more concrete or real

Slide 43

Slide 43 text

reactive programming reifies event-driven software

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

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) }

Slide 46

Slide 46 text

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")

Slide 47

Slide 47 text

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) })

Slide 48

Slide 48 text

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 })

Slide 49

Slide 49 text

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 }

Slide 50

Slide 50 text

SO… HOW DO I DO THIS?

Slide 51

Slide 51 text

ReactiveCocoa ReactiveExtensions RxJava Bacon.js Elm github.com/ReactiveCocoa/ReactiveCocoa github.com/Reactive-Extensions github.com/ReactiveX/RxJava github.com/baconjs/bacon.js elm-lang.org

Slide 52

Slide 52 text

SOURCES FURTHER READING

Slide 53

Slide 53 text

Missing RX Intro Input & Output RxMarbles Other j.mp/missingrxintro j.mp/joshaberio rxmarbles.com pinboard.in/u:andrewsardone/t:reactive

Slide 54

Slide 54 text

B Y E (photo by Chris Dzombak) (puppy by Shane Bliemaster)