SlideShare a Scribd company logo
Reactive Cocoa
“A framework for composing and transforming
streams of values.”
Reactive Cocoa

Reactive Cocoa is a third-party Objective C
framework which has received some interested and a
community-following.
Functional Programming
Functional programming is a style of programming based on a formal model of computation called lambda calculus.
Some programming languages such as Lisp (and its children Scheme and Clojure), Haskell, Scala, ML called functional
programming languages because they are very amenable to this style of programming, although it can technically be
done with many other languages.
Languages like C, Objective C, and Java are called imperative because programs are often written in a different style and
these languages support that style.
For our purposes, functional programming means programs are composed of functions.
f(x) = x+5, g(x) = x+2
We can evaluate functions which are composed together by substitution.
f(g(4))
f(4+2)
f(6)
6+5
11
Given an input, we always get the same output.
Functional programming tend to operate on lists, which are seen as a sequences of values.
list = (1, 2, 3)
Calling it a sequence is important. Generally, lists are treated as the first element (head) and
the rest of the elements besides the head.
list.head = 1, list.tail = (2, 3)
This is different from an array where any element can be accessed at one time.
list[2]
This approach has the advantage of allowing some programs to be written in a recursive
style.
int maxValue(currentMax, list) {
if (list.empty) return currentMax
else if (list.head > currentMax) return maxValue(list.head, list.tail)
else maxValue(currentMax, list.tail)
}

Also: infinite streams.
The ability to have that kind of mutable state is forbidden in
pure functional programming. In practice, pure functional
programming does not have assignment.
(Maybe languages that support functional programming do,
in fact, allow assignment.)
It turns out, this is an excellent feature for designing
applications that work with concurrency. This is one
reason why functional programming has received a lot of
interest recently (and why, for example, Apple brought
blocks to C / Objective C in 2009).
But following this rule presents a challenge for
working with user input.
Imagine you wanted to write a function which gets
keystrokes one character at a time.
char getCharacter();
This function is not really compatible with functional
styles of programming, because it might return any
character from the same input (in this case,
nothing).
- (int)maxOfX:(int)x Y:(int)y {
if (x > y)
return x;
else
return y;
}

[self maxOfX:50 andY:75];

//////
//////

@property (nonatomic) int x;
[self maxOfXandY:75];

- (void)buttonPressed:(id)sender {
if (self.x == 0)
self.x = 100;
else
self.x = 0;
}
- (int)maxOfXandY:(int)y {
if (self.x > y)
return self.x;
else
return y;
}
Functional Reactive Programming
There are a lot of various implementations of
Functional Reactive Programming (e.g. Elm).
It handles the problems of dealing with user input by
treating it as the parameters of functions, executing
them again and again as the input changes.
function ( ) {
}
Reactive Cocoa
Based on Microsoft’s Rx Extensions.
Developed by engineers at GitHub.
It is quite large. It may be best to consider it an
extension to the Objective C language.
My plan is only to demonstrate some practical
examples, focusing on the concept of signals.
Signals
[RACObserve(self, firstName) subscribeNext:^(NSString *newName) {
NSLog(@"%@", newName);
}];
self.firstName = @"Matt";
[[RACObserve(self, lastName)
filter:^(NSString *newName) {
return [newName hasPrefix:@"Gill"];
}]
subscribeNext:^(NSString *newName) {
NSLog(@"%@", newName);
}];
self.lastName = @"Jones";
self.lastName = @"Gillingham";
self.lastName = @"Mayberry";
Combine Signals
RACSignal *signal1 = @[@(1)].rac_sequence.signal;
RACSignal *signal2 = @[@(2)].rac_sequence.signal;
[[RACSignal
merge:@[signal1, signal2]]
subscribeCompleted:^{
NSLog(@"They're both done!");
}];
[signal1 subscribeNext:^(id x) {
NSLog(@"Signal 1: %@", x);
}];
[signal2 subscribeNext:^(id x) {
NSLog(@"Signal 2: %@", x);
}];
UI as Signals
self.textField = [[UITextField alloc] initWithFrame:
CGRectMake(10.0f, 10.0f, 100.0f, 44.0f)
];
[self.textField.rac_textSignal subscribeNext:^(id x) {
NSLog(@“1: %@", x);
}];
[self.textField.rac_textSignal subscribeNext:^(id x) {
NSLog(@“2: %@", x);
}];
[self.view addSubview:self.textField];
A More Complex Example
self.email1Field = [[UITextField alloc] initWithFrame:CGRectMake(30.0f, 130.0f, 200.0f,
30.0f)];
self.email1Field.borderStyle = UITextBorderStyleLine;
[self.view addSubview:self.email1Field];
self.email2Field = [[UITextField alloc] initWithFrame:CGRectMake(30.0f, 165.0f, 200.0f,
30.0f)];
self.email2Field.borderStyle = UITextBorderStyleLine;
[self.view addSubview:self.email2Field];
self.submitButton = [UIButton buttonWithType:UIButtonTypeSystem];
[self.submitButton setTitle:@"Submit" forState:UIControlStateNormal];
self.submitButton.frame = CGRectMake(30.0f, 200.0f, 200.0f, 30.0f);
self.submitButton.enabled = NO;
[[[[RACSignal
merge:@[self.email1Field.rac_textSignal, self.email2Field.rac_textSignal]]
map:^id(id value) {
return @([self.email1Field.text isEqualToString:self.email2Field.text]);
}]
distinctUntilChanged]
subscribeNext:^(NSNumber *equal) {
self.submitButton.enabled = [equal boolValue];
}];
[self.view addSubview:self.email1Field];
[self.view addSubview:self.email2Field];
[self.view addSubview:self.submitButton];
Note on Concurrency
No two subscription blocks can be called
simultaneously to avoid deadlock.
You can control which thread the subscription block is
called on with RACScheduler
This allows for signals to mimic an
NSOperationQueue.
Conclusion
ReactiveCocoa is based on some relatively abstract
concepts but these concepts turn out to be very, very
powerful in practice.
Signals, for example, may be able to replace KVO,
operation queues and delegation in an app. I have
barely touched the available feature set in these
examples.
Conclusion
It does require a lot of abstract thinking to determine
how to model your apps behavior as a series of
signals.
For this reason, there is a learning curve associated
with it.
Personally, I am very likely use it in the future.

More Related Content

PDF
Functional Programming in JavaScript
PPTX
Function overloading and overriding
PDF
Java 8 lambda expressions
PDF
Intro to functional programming
PDF
Functional Programming with JavaScript
PDF
Functional JavaScript Fundamentals
PDF
PPT
Inroduction to r
Functional Programming in JavaScript
Function overloading and overriding
Java 8 lambda expressions
Intro to functional programming
Functional Programming with JavaScript
Functional JavaScript Fundamentals
Inroduction to r

What's hot (18)

PPT
3 Function Overloading
PPT
Introduction To Functional Programming
PDF
Intro to functional programming
PDF
Functional programming 101
PPTX
Java8: what's new and what's hot
PDF
PDF
Functional programming java
ODP
Functors, Applicatives and Monads In Scala
PPTX
Function overloading in c++
ODP
Pattern Matching - at a glance
PPT
Scala functions
DOCX
C programming language working with functions 1
PPTX
Functional Programming with JavaScript
PPTX
Operator overloading
PDF
Implicit conversion and parameters
ODP
Functions & closures
PPTX
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
ODP
Functional programming with Scala
3 Function Overloading
Introduction To Functional Programming
Intro to functional programming
Functional programming 101
Java8: what's new and what's hot
Functional programming java
Functors, Applicatives and Monads In Scala
Function overloading in c++
Pattern Matching - at a glance
Scala functions
C programming language working with functions 1
Functional Programming with JavaScript
Operator overloading
Implicit conversion and parameters
Functions & closures
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Functional programming with Scala
Ad

Similar to Reactive cocoa (20)

PDF
Reactive Cocoa
PDF
Functional Reactive Programming (CocoaHeads Bratislava)
PDF
Learn You a ReactiveCocoa for Great Good
PDF
Introduction to reactive programming & ReactiveCocoa
PDF
Reactive cocoa cocoaheadsbe_2014
PDF
An Introduction to Reactive Cocoa
PDF
Reactive Cocoa Lightning Talk
PDF
ReactiveCocoa - Functional Reactive Programming concepts in iOS
PDF
Reactive cocoa
PDF
ReactiveCocoa Goodness - Part I of II
PDF
«ReactiveCocoa и MVVM» — Николай Касьянов, SoftWear
PPT
Lviv MD Day 2015 Павло Захаров "Reactive cocoa: paradigm shift"
PDF
Introduction To Functional Reactive Programming Poznan
PDF
ReactiveCocoa in Practice
PDF
Functional programming
PDF
Reactive cocoa made Simple with Swift
PDF
Introduction to Functional Reactive Programming
PPT
Reactive programming with examples
PPTX
Reactive cocoa 101改
PPTX
Mario Fusco - Reactive programming in Java - Codemotion Milan 2017
Reactive Cocoa
Functional Reactive Programming (CocoaHeads Bratislava)
Learn You a ReactiveCocoa for Great Good
Introduction to reactive programming & ReactiveCocoa
Reactive cocoa cocoaheadsbe_2014
An Introduction to Reactive Cocoa
Reactive Cocoa Lightning Talk
ReactiveCocoa - Functional Reactive Programming concepts in iOS
Reactive cocoa
ReactiveCocoa Goodness - Part I of II
«ReactiveCocoa и MVVM» — Николай Касьянов, SoftWear
Lviv MD Day 2015 Павло Захаров "Reactive cocoa: paradigm shift"
Introduction To Functional Reactive Programming Poznan
ReactiveCocoa in Practice
Functional programming
Reactive cocoa made Simple with Swift
Introduction to Functional Reactive Programming
Reactive programming with examples
Reactive cocoa 101改
Mario Fusco - Reactive programming in Java - Codemotion Milan 2017
Ad

More from gillygize (8)

PDF
Some Stuff I was thinking about state machines and types
PDF
Manual Layout Revisited
PPT
Optimize llvm
PPT
Connecting to a REST API in iOS
PPT
State ofappdevelopment
KEY
ViewController/State
KEY
Two-StageCreation
KEY
Categories
Some Stuff I was thinking about state machines and types
Manual Layout Revisited
Optimize llvm
Connecting to a REST API in iOS
State ofappdevelopment
ViewController/State
Two-StageCreation
Categories

Recently uploaded (20)

PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Getting Started with Data Integration: FME Form 101
PPTX
Tartificialntelligence_presentation.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Empathic Computing: Creating Shared Understanding
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Machine Learning_overview_presentation.pptx
PPTX
Spectroscopy.pptx food analysis technology
PPTX
cloud_computing_Infrastucture_as_cloud_p
Agricultural_Statistics_at_a_Glance_2022_0.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Machine learning based COVID-19 study performance prediction
Programs and apps: productivity, graphics, security and other tools
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Getting Started with Data Integration: FME Form 101
Tartificialntelligence_presentation.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Empathic Computing: Creating Shared Understanding
SOPHOS-XG Firewall Administrator PPT.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Spectral efficient network and resource selection model in 5G networks
Reach Out and Touch Someone: Haptics and Empathic Computing
Digital-Transformation-Roadmap-for-Companies.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Machine Learning_overview_presentation.pptx
Spectroscopy.pptx food analysis technology
cloud_computing_Infrastucture_as_cloud_p

Reactive cocoa

  • 1. Reactive Cocoa “A framework for composing and transforming streams of values.”
  • 2. Reactive Cocoa Reactive Cocoa is a third-party Objective C framework which has received some interested and a community-following.
  • 3. Functional Programming Functional programming is a style of programming based on a formal model of computation called lambda calculus. Some programming languages such as Lisp (and its children Scheme and Clojure), Haskell, Scala, ML called functional programming languages because they are very amenable to this style of programming, although it can technically be done with many other languages. Languages like C, Objective C, and Java are called imperative because programs are often written in a different style and these languages support that style. For our purposes, functional programming means programs are composed of functions. f(x) = x+5, g(x) = x+2 We can evaluate functions which are composed together by substitution. f(g(4)) f(4+2) f(6) 6+5 11 Given an input, we always get the same output.
  • 4. Functional programming tend to operate on lists, which are seen as a sequences of values. list = (1, 2, 3) Calling it a sequence is important. Generally, lists are treated as the first element (head) and the rest of the elements besides the head. list.head = 1, list.tail = (2, 3) This is different from an array where any element can be accessed at one time. list[2] This approach has the advantage of allowing some programs to be written in a recursive style. int maxValue(currentMax, list) { if (list.empty) return currentMax else if (list.head > currentMax) return maxValue(list.head, list.tail) else maxValue(currentMax, list.tail) } Also: infinite streams.
  • 5. The ability to have that kind of mutable state is forbidden in pure functional programming. In practice, pure functional programming does not have assignment. (Maybe languages that support functional programming do, in fact, allow assignment.) It turns out, this is an excellent feature for designing applications that work with concurrency. This is one reason why functional programming has received a lot of interest recently (and why, for example, Apple brought blocks to C / Objective C in 2009).
  • 6. But following this rule presents a challenge for working with user input. Imagine you wanted to write a function which gets keystrokes one character at a time. char getCharacter(); This function is not really compatible with functional styles of programming, because it might return any character from the same input (in this case, nothing).
  • 7. - (int)maxOfX:(int)x Y:(int)y { if (x > y) return x; else return y; } [self maxOfX:50 andY:75]; ////// ////// @property (nonatomic) int x; [self maxOfXandY:75]; - (void)buttonPressed:(id)sender { if (self.x == 0) self.x = 100; else self.x = 0; } - (int)maxOfXandY:(int)y { if (self.x > y) return self.x; else return y; }
  • 8. Functional Reactive Programming There are a lot of various implementations of Functional Reactive Programming (e.g. Elm). It handles the problems of dealing with user input by treating it as the parameters of functions, executing them again and again as the input changes. function ( ) { }
  • 9. Reactive Cocoa Based on Microsoft’s Rx Extensions. Developed by engineers at GitHub. It is quite large. It may be best to consider it an extension to the Objective C language. My plan is only to demonstrate some practical examples, focusing on the concept of signals.
  • 10. Signals [RACObserve(self, firstName) subscribeNext:^(NSString *newName) { NSLog(@"%@", newName); }]; self.firstName = @"Matt"; [[RACObserve(self, lastName) filter:^(NSString *newName) { return [newName hasPrefix:@"Gill"]; }] subscribeNext:^(NSString *newName) { NSLog(@"%@", newName); }]; self.lastName = @"Jones"; self.lastName = @"Gillingham"; self.lastName = @"Mayberry";
  • 11. Combine Signals RACSignal *signal1 = @[@(1)].rac_sequence.signal; RACSignal *signal2 = @[@(2)].rac_sequence.signal; [[RACSignal merge:@[signal1, signal2]] subscribeCompleted:^{ NSLog(@"They're both done!"); }]; [signal1 subscribeNext:^(id x) { NSLog(@"Signal 1: %@", x); }]; [signal2 subscribeNext:^(id x) { NSLog(@"Signal 2: %@", x); }];
  • 12. UI as Signals self.textField = [[UITextField alloc] initWithFrame: CGRectMake(10.0f, 10.0f, 100.0f, 44.0f) ]; [self.textField.rac_textSignal subscribeNext:^(id x) { NSLog(@“1: %@", x); }]; [self.textField.rac_textSignal subscribeNext:^(id x) { NSLog(@“2: %@", x); }]; [self.view addSubview:self.textField];
  • 13. A More Complex Example self.email1Field = [[UITextField alloc] initWithFrame:CGRectMake(30.0f, 130.0f, 200.0f, 30.0f)]; self.email1Field.borderStyle = UITextBorderStyleLine; [self.view addSubview:self.email1Field]; self.email2Field = [[UITextField alloc] initWithFrame:CGRectMake(30.0f, 165.0f, 200.0f, 30.0f)]; self.email2Field.borderStyle = UITextBorderStyleLine; [self.view addSubview:self.email2Field]; self.submitButton = [UIButton buttonWithType:UIButtonTypeSystem]; [self.submitButton setTitle:@"Submit" forState:UIControlStateNormal]; self.submitButton.frame = CGRectMake(30.0f, 200.0f, 200.0f, 30.0f); self.submitButton.enabled = NO; [[[[RACSignal merge:@[self.email1Field.rac_textSignal, self.email2Field.rac_textSignal]] map:^id(id value) { return @([self.email1Field.text isEqualToString:self.email2Field.text]); }] distinctUntilChanged] subscribeNext:^(NSNumber *equal) { self.submitButton.enabled = [equal boolValue]; }]; [self.view addSubview:self.email1Field]; [self.view addSubview:self.email2Field]; [self.view addSubview:self.submitButton];
  • 14. Note on Concurrency No two subscription blocks can be called simultaneously to avoid deadlock. You can control which thread the subscription block is called on with RACScheduler This allows for signals to mimic an NSOperationQueue.
  • 15. Conclusion ReactiveCocoa is based on some relatively abstract concepts but these concepts turn out to be very, very powerful in practice. Signals, for example, may be able to replace KVO, operation queues and delegation in an app. I have barely touched the available feature set in these examples.
  • 16. Conclusion It does require a lot of abstract thinking to determine how to model your apps behavior as a series of signals. For this reason, there is a learning curve associated with it. Personally, I am very likely use it in the future.