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

λ is coming to Obj-C – ReactiveCocoa

λ is coming to Obj-C – ReactiveCocoa

Vladimir Burdukov

February 24, 2014
Tweet

More Decks by Vladimir Burdukov

Other Decks in Programming

Transcript

  1. Pure function • result doesn’t depend on any state •

    function doesn’t change any external state.
  2. Pure function def sqr(x: Int): Int = x * x

    //> sqr: (x: Int)Int sqr(2) /> res4: Int = 4 def add1(x: Int): Int = x + 1 //> add1: (x: Int)Int add1(4) //> res3: Int = 5 add1(add1(4)) //> res4: Int = 6
  3. Higher-order functions def sqr(x: Int): Int = x * x

    def sumF(x: Int, y: Int, f: Int => Int) = f(x) + f(y) sumF(3, 4, sqr) //> res5: Int = 25
  4. Higher-order functions def sqr(x: Int): Int = x * x

    def sumF(x: Int, y: Int, f: Int => Int) = f(x) + f(y) sumF(3, 4, sqr) //> res5: Int = 25 sumF(-9, 16, abs) //> res6: Int = 25
  5. Tail recursion Lists val foo: List[Int] = 1 to 5

    toList //> foo : List[Int] = List(1, 2, 3, 4, 5) val foohead = foo.head //> foohead : Int = 1 val footail = foo.tail //> footail : List[Int] = List(2, 3, 4, 5)
  6. Tail recursion def sumList(xs: List[Int]): Int = if (xs ==

    Nil) return 0 else return xs.head + sumList(xs.tail) // > sumList: (xs: List[Int])Int ! sumList(foo) //> res3: Int = 15
  7. Composition def f(a: String): String = "f(" + a +

    ")" f("5") //> res11: String = f(5) def g(a: String): String = "g(" + a + “)" g("5") //> res12: String = g(5) f(g("5")) //> res13: String = f(g(5)) def fg = f _ compose g _ //> fg: => String => String fg("5") //> res14: String = f(g(5))
  8. RACSignal UISwitch *settingSwitch = [[UISwitch alloc] init]; RACSignal *switchSignal =

    [settingSwitch rac_signalForControlEvents:UIControlEventValueChanged]; [switchSignal subscribeNext:^(UISwitch *x) { NSLog(@"SWITCH %@", x.on); }]; //SWITCH 0 //SWITCH 1 //SWITCH 0
  9. ReactiveCocoa UI extensions • UIActionSheet / UIAlertView • UIButton •

    UITableViewCell • UIControl (rac_signalForControlEvents:) • UIGestureRecognizer • UITextField
  10. UIAlertView Signal UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Something goes

    wrong" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:@"Retry", nil]; ! [[alertView rac_buttonClickedSignal] subscribeNext:^(NSNumber *buttonNumber) { NSLog(@"alert button: %@", [alertView buttonTitleAtIndex:buttonNumber.intValue]); }];
  11. UIButton Command RACCommand *loginCommand = [[RACCommand alloc] initWithEnabled:[RACSignal combineLatest:@[loginField.rac_textSignal, passwordField.rac_textSignal]

    reduce:^id(NSString *username, NSString *password) { return @(username.length > 0 && password.length > 0); }] signalBlock:^RACSignal *(id input) { return [[JMHSync shared] loginUser:self.loginField.text withPassword:self.passwordField.text]; }]; ! self.createButton.rac_command = loginCommand;