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

Function Reactive Programming in iOS

sammyd
July 03, 2014

Function Reactive Programming in iOS

A brief overview into functional reactive programming, with some specific use cases in ReactiveCocoa.

sammyd

July 03, 2014
Tweet

More Decks by sammyd

Other Decks in Technology

Transcript

  1. computer programming is a process that leads from an original

    formulation of a computing problem to executable programs programming
  2. example username password s sa sam p pa pas pass

    submit username password 1 2 3 1 2 3 4 string.length map
  3. example username password s sa sam p pa pas pass

    submit username password 3 3 4 greater than 2 filter
  4. example let usernameSignal = tf.rac_textSignal() .mapAs({ (value: NSString) -> NSNumber

    in return value.length }) .filterAs({ (value: NSNumber) -> Bool in return value.intValue > 2 })
  5. example let usernameSignal = tf.rac_textSignal() .mapAs({ (value: NSString) -> NSNumber

    in return value.length }) .filterAs({ (value: NSNumber) -> Bool in return value.intValue > 2 })
  6. example let usernameSignal = tf.rac_textSignal() .mapAs({ (value: NSString) -> NSNumber

    in return value.length }) .filterAs({ (value: NSNumber) -> Bool in return value.intValue > 2 })
  7. example RESPONSE: { "type":"unspecified", "content":"Wikipedia talk:Articles for creation/Bonnie ZoBell", “time":"2014-05-15T14:45:59.175Z"

    } RESPONSE: { "type":"unspecified", "content":"Wikipedia:WikiProject Spam/LinkReports/blog.wifirst.fr", “time":"2014-05-15T14:45:59.247Z" } RESPONSE: { "type":"special", "content":"", “time":"2014-05-15T14:46:00.262Z" } RESPONSE: { "type":"unspecified", "content":"Manhattan Film Academy", “time":"2014-05-15T14:46:00.828Z" }
  8. example wsConnector.messages .filterAs({ (dict: NSDictionary) in return (dict["type"] as NSString).isEqualToString("unspecified")

    }) .mapAs({ (dict: NSDictionary) -> NSString in return dict["content"] as NSString }) .deliverOn(RACScheduler.mainThreadScheduler()) .subscribeNextAs({(value: NSString) in self.tickerLabel.text = value })
  9. example wsConnector.messages .filterAs({ (dict: NSDictionary) in return (dict["type"] as NSString).isEqualToString("unspecified")

    }) .mapAs({ (dict: NSDictionary) -> NSString in return dict["content"] as NSString }) .deliverOn(RACScheduler.mainThreadScheduler()) .subscribeNextAs({(value: NSString) in self.tickerLabel.text = value })
  10. example wsConnector.messages .filterAs({ (dict: NSDictionary) in return (dict["type"] as NSString).isEqualToString("unspecified")

    }) .mapAs({ (dict: NSDictionary) -> NSString in return dict["content"] as NSString }) .deliverOn(RACScheduler.mainThreadScheduler()) .subscribeNextAs({(value: NSString) in self.tickerLabel.text = value })
  11. example wsConnector.messages .bufferWithTime(5, onScheduler: scheduler) .mapAs({ (value: RACTuple) -> NSNumber

    in return NSNumber(double: Double(value.count) / 5.0) }) .deliverOn(RACScheduler.mainThreadScheduler()) .logNext() .subscribeNext({(x: AnyObject!) in self.datasource!.appendValue(x as NSNumber) })
  12. example wsConnector.messages .bufferWithTime(5, onScheduler: scheduler) .mapAs({ (value: RACTuple) -> NSNumber

    in return NSNumber(double: Double(value.count) / 5.0) }) .deliverOn(RACScheduler.mainThreadScheduler()) .logNext() .subscribeNext({(x: AnyObject!) in self.datasource!.appendValue(x as NSNumber) })
  13. example wsConnector.messages .bufferWithTime(5, onScheduler: scheduler) .mapAs({ (value: RACTuple) -> NSNumber

    in return NSNumber(double: Double(value.count) / 5.0) }) .deliverOn(RACScheduler.mainThreadScheduler()) .logNext() .subscribeNext({(x: AnyObject!) in self.datasource!.appendValue(x as NSNumber) })
  14. example wsConnector.messages .filterAs({ (value: NSDictionary) -> Bool in return (value["type"]

    as NSString).isEqualToString("newuser") }) .mapAs({ (value: NSDictionary) -> SChartAnnotation in return self.createNewUserAnnotation(value["time"]) }) .deliverOn(RACScheduler.mainThreadScheduler()) .subscribeNextAs({ (value: SChartAnnotation) in self.chart.addAnnotation(value) self.chart.redrawChart() })
  15. example wsConnector.messages .filterAs({ (value: NSDictionary) -> Bool in return (value["type"]

    as NSString).isEqualToString("newuser") }) .mapAs({ (value: NSDictionary) -> SChartAnnotation in return self.createNewUserAnnotation(value["time"]) }) .deliverOn(RACScheduler.mainThreadScheduler()) .subscribeNextAs({ (value: SChartAnnotation) in self.chart.addAnnotation(value) self.chart.redrawChart() })
  16. example wsConnector.messages .filterAs({ (value: NSDictionary) -> Bool in return (value["type"]

    as NSString).isEqualToString("newuser") }) .mapAs({ (value: NSDictionary) -> SChartAnnotation in return self.createNewUserAnnotation(value["time"]) }) .deliverOn(RACScheduler.mainThreadScheduler()) .subscribeNextAs({ (value: SChartAnnotation) in self.chart.addAnnotation(value) self.chart.redrawChart() })