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

Functional Reactive Programming in an imperative world

NachoSoto
September 08, 2015

Functional Reactive Programming in an imperative world

NachoSoto

September 08, 2015
Tweet

Other Decks in Programming

Transcript

  1. So what's wrong with this? private var lastScheduledWord: dispatch_cancelable_block_t? private

    var lastRequest: NSURLSessionTask? private var numberOfRetries = 0 func newSearchString(word: String) { cancel_block(lastScheduledWord) lastRequest?.cancel() lastRequest = nil lastScheduledWord = dispatch_after_delay(THROTTLE_SECONDS) { self.lastRequest = fetchWord(word, success: { result in self.displayResult(word, result) }, failure: { error in if self.numberOfRetries++ < NUMBER_OF_RETRIES { self.newSearchString(word) } else { self.numberOfRetries = 0 self.displayError(error) } }) } }
  2. So what's wrong with this? • Does it work? •

    Are there bugs? • Are there race conditions? What thread is each thing running on? • How can we add a new feature without adding (or multiplying) the complexity?
  3. let keyStrokes = searchField.keyStrokes() keyStrokes .throttle(THROTTLE_SECONDS) .flatMap { word in

    fetchWord(word) .retry(NUMBER_OF_RETRIES) .takeUntil(keyStrokes) .map { result in (word, result) } } .start( next: displayResult, error: displayError )
  4. So what's right with this? • Does it work? •

    Are there bugs? • Are there race conditions? What thread is each thing running on? • How can we add a new feature without adding (or multiplying) the complexity?
  5. Credits • Simple Made Easy by Rich Hickey. • Enemy

    of the state by Justin Spahr-Summers. • RxJS talk by Netflix.