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

December 07, 2013
Tweet

More Decks by Vladimir Burdukov

Other Decks in Programming

Transcript

  1. Composition package cocoaheads ! object cocoaheads { def fib(n: Int):

    List[Int] = { def tail(n: Int, p: Int, m: Int): List[Int] = if (n == 1) List(p) else p :: tail(n - 1, m, p + m) tail(n, 1, 1) } //> fib: (n: Int)List[Int] fib(4) //> res0: List[Int] = List(1, 1, 2, 3) fib(4).zip(fib(4).map((x: Int) => x * x)) //> res1: List[(Int, Int)] = List((1,1), (1,1), (2,4), (3,9)) fib(4).foldLeft(0)((m: Int, n: Int) => m + n) //> res2: Int = 7 } http://bcomposes.wordpress.com/2011/08/20/fun-with- function-composition-in-scala/
  2. RACSignal RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { if

    (value) { [subscriber sendNext:value]; [subscriber sendCompleted]; } else { [subscriber sendError:error]; } return nil; }];
  3. RACSignal [signal subscribeNext:^(id x) { NSLog(@"SIGNAL NEW VALUE: %@", x);

    } error:^(NSError *error) { NSLog(@"SIGNAL ERROR: %@", error); } completed:^{ NSLog(@"SIGNAL COMPLETED"); }]; RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { if (value) { [subscriber sendNext:value]; [subscriber sendCompleted]; } else { [subscriber sendError:error]; } return nil; }];
  4. RACSignal *loginEditingSignal = [[self.loginField rac_signalForControlEvents:UIControlEventEditingChanged] map:^id(UITextField *x) { return @(x.isEditing);

    }]; ! RACSignal *passwordEditingSignal = [[self.passwordField rac_signalForControlEvents:UIControlEventEditingChanged] map:^id(UITextField *x) { return @(x.isEditing); }]; RAC(self.tapGestureRecognizer, enabled) = [RACSignal combineLatest:@[loginEditingSignal, passwordEditingSignal] reduce:^id(NSNumber *loginIsEditing, NSNumber *passwordIsEditing){ return @([loginIsEditing boolValue] || [passwordIsEditing boolValue]); }]; RACSignal Combining
  5. RACSequence RACSequence *fib(int n) { return fibTail(n, 1, 1); }

    ! RACSequence *fibTail(int n, int p, int m) { return [RACSequence sequenceWithHeadBlock:^NSNumber *{ return @(p); } tailBlock:^RACSequence *{ if (n == 1) { return [RACSequence empty]; } else { return fibTail(n - 1, m, p + m); } }]; } ! NSLog(@"%@", fib(4).array); // [1, 1, 2, 3] ! NSLog(@"%@", [fib(4) zipWith:[fib(4) map:^NSNumber *(NSNumber *value) { return @([value integerValue] * [value integerValue]); }]].array); // [(1, 1), (1, 1), (2, 4), (3, 9)] ! NSLog(@"%@", [fib(4) foldLeftWithStart:@0 reduce:^NSNumber *(NSNumber *accumulator, NSNumber *value) { return @(accumulator.integerValue + value.integerValue); }]); // 7
  6. ReactiveCocoa UI extensions • UIActionSheet / UIAlertView • UIButton •

    UITableViewCell • UIControl (rac_signalForControlEvents:) • UIGestureRecognizer • UITextField