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

What's hot

3 Function Overloading
3 Function Overloading3 Function Overloading
3 Function Overloading
Praveen M Jigajinni
 
Introduction To Functional Programming
Introduction To Functional ProgrammingIntroduction To Functional Programming
Introduction To Functional Programming
newmedio
 
Intro to functional programming
Intro to functional programmingIntro to functional programming
Intro to functional programming
Assaf Gannon
 
Functional programming 101
Functional programming 101Functional programming 101
Functional programming 101
Maneesh Chaturvedi
 
Java8: what's new and what's hot
Java8: what's new and what's hotJava8: what's new and what's hot
Java8: what's new and what's hot
Sergii Maliarov
 
Java8
Java8Java8
Functional programming java
Functional programming javaFunctional programming java
Functional programming java
Maneesh Chaturvedi
 
Functors, Applicatives and Monads In Scala
Functors, Applicatives and Monads In ScalaFunctors, Applicatives and Monads In Scala
Functors, Applicatives and Monads In Scala
Knoldus Inc.
 
Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++
Learn By Watch
 
Pattern Matching - at a glance
Pattern Matching - at a glancePattern Matching - at a glance
Pattern Matching - at a glance
Knoldus Inc.
 
Scala functions
Scala functionsScala functions
Scala functions
Knoldus Inc.
 
C programming language working with functions 1
C programming language working with functions 1C programming language working with functions 1
C programming language working with functions 1
Jeevan Raj
 
Functional Programming with JavaScript
Functional Programming with JavaScriptFunctional Programming with JavaScript
Functional Programming with JavaScript
Aung Baw
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Burhan Ahmed
 
Implicit conversion and parameters
Implicit conversion and parametersImplicit conversion and parameters
Implicit conversion and parameters
Knoldus Inc.
 
Functions & closures
Functions & closuresFunctions & closures
Functions & closures
Knoldus Inc.
 
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Harmeet Singh(Taara)
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with Scala
Neelkanth Sachdeva
 

What's hot (18)

3 Function Overloading
3 Function Overloading3 Function Overloading
3 Function Overloading
 
Introduction To Functional Programming
Introduction To Functional ProgrammingIntroduction To Functional Programming
Introduction To Functional Programming
 
Intro to functional programming
Intro to functional programmingIntro to functional programming
Intro to functional programming
 
Functional programming 101
Functional programming 101Functional programming 101
Functional programming 101
 
Java8: what's new and what's hot
Java8: what's new and what's hotJava8: what's new and what's hot
Java8: what's new and what's hot
 
Java8
Java8Java8
Java8
 
Functional programming java
Functional programming javaFunctional programming java
Functional programming java
 
Functors, Applicatives and Monads In Scala
Functors, Applicatives and Monads In ScalaFunctors, Applicatives and Monads In Scala
Functors, Applicatives and Monads In Scala
 
Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++
 
Pattern Matching - at a glance
Pattern Matching - at a glancePattern Matching - at a glance
Pattern Matching - at a glance
 
Scala functions
Scala functionsScala functions
Scala functions
 
C programming language working with functions 1
C programming language working with functions 1C programming language working with functions 1
C programming language working with functions 1
 
Functional Programming with JavaScript
Functional Programming with JavaScriptFunctional Programming with JavaScript
Functional Programming with JavaScript
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Implicit conversion and parameters
Implicit conversion and parametersImplicit conversion and parameters
Implicit conversion and parameters
 
Functions & closures
Functions & closuresFunctions & closures
Functions & closures
 
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with Scala
 

Similar to Reactive cocoa

Java 8
Java 8Java 8
Java 8
vilniusjug
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
Martin Odersky
 
Java gets a closure
Java gets a closureJava gets a closure
Java gets a closure
Tomasz Kowalczewski
 
An Introduction to Reactive Cocoa
An Introduction to Reactive CocoaAn Introduction to Reactive Cocoa
An Introduction to Reactive Cocoa
SmartLogic
 
PHP 5.3 Part 2 - Lambda Functions & Closures
PHP 5.3 Part 2 - Lambda Functions & ClosuresPHP 5.3 Part 2 - Lambda Functions & Closures
PHP 5.3 Part 2 - Lambda Functions & Closures
melechi
 
Scala in a nutshell by venkat
Scala in a nutshell by venkatScala in a nutshell by venkat
Scala in a nutshell by venkat
Venkateswaran Kandasamy
 
DML Syntax and Invocation process
DML Syntax and Invocation processDML Syntax and Invocation process
DML Syntax and Invocation process
Arvind Surve
 
S1 DML Syntax and Invocation
S1 DML Syntax and InvocationS1 DML Syntax and Invocation
S1 DML Syntax and Invocation
Arvind Surve
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
JavaScript.pptx
JavaScript.pptxJavaScript.pptx
JavaScript.pptx
Govardhan Bhavani
 
Scala is java8.next()
Scala is java8.next()Scala is java8.next()
Scala is java8.next()
daewon jeong
 
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time APIModern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
Ganesh Samarthyam
 
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of TonguesChoose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
CHOOSE
 
WD programs descriptions.docx
WD programs descriptions.docxWD programs descriptions.docx
WD programs descriptions.docx
anjani pavan kumar
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
Jesper Kamstrup Linnet
 
Lambdas and Streams Master Class Part 2
Lambdas and Streams Master Class Part 2Lambdas and Streams Master Class Part 2
Lambdas and Streams Master Class Part 2
José Paumard
 
Dax Declarative Api For Xml
Dax   Declarative Api For XmlDax   Declarative Api For Xml
Dax Declarative Api For Xml
Lars Trieloff
 

Similar to Reactive cocoa (20)

Java 8
Java 8Java 8
Java 8
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
 
Java gets a closure
Java gets a closureJava gets a closure
Java gets a closure
 
An Introduction to Reactive Cocoa
An Introduction to Reactive CocoaAn Introduction to Reactive Cocoa
An Introduction to Reactive Cocoa
 
PHP 5.3 Part 2 - Lambda Functions & Closures
PHP 5.3 Part 2 - Lambda Functions & ClosuresPHP 5.3 Part 2 - Lambda Functions & Closures
PHP 5.3 Part 2 - Lambda Functions & Closures
 
Scala in a nutshell by venkat
Scala in a nutshell by venkatScala in a nutshell by venkat
Scala in a nutshell by venkat
 
DML Syntax and Invocation process
DML Syntax and Invocation processDML Syntax and Invocation process
DML Syntax and Invocation process
 
S1 DML Syntax and Invocation
S1 DML Syntax and InvocationS1 DML Syntax and Invocation
S1 DML Syntax and Invocation
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
JavaScript.pptx
JavaScript.pptxJavaScript.pptx
JavaScript.pptx
 
Scala is java8.next()
Scala is java8.next()Scala is java8.next()
Scala is java8.next()
 
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time APIModern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
 
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of TonguesChoose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
 
WD programs descriptions.docx
WD programs descriptions.docxWD programs descriptions.docx
WD programs descriptions.docx
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
 
Lambdas and Streams Master Class Part 2
Lambdas and Streams Master Class Part 2Lambdas and Streams Master Class Part 2
Lambdas and Streams Master Class Part 2
 
Dax Declarative Api For Xml
Dax   Declarative Api For XmlDax   Declarative Api For Xml
Dax Declarative Api For Xml
 

More from gillygize

Some Stuff I was thinking about state machines and types
Some Stuff I was thinking about state machines and typesSome Stuff I was thinking about state machines and types
Some Stuff I was thinking about state machines and types
gillygize
 
Manual Layout Revisited
Manual Layout RevisitedManual Layout Revisited
Manual Layout Revisited
gillygize
 
Optimize llvm
Optimize llvmOptimize llvm
Optimize llvmgillygize
 
Connecting to a REST API in iOS
Connecting to a REST API in iOSConnecting to a REST API in iOS
Connecting to a REST API in iOS
gillygize
 
State ofappdevelopment
State ofappdevelopmentState ofappdevelopment
State ofappdevelopment
gillygize
 
ViewController/State
ViewController/StateViewController/State
ViewController/State
gillygize
 
Two-StageCreation
Two-StageCreationTwo-StageCreation
Two-StageCreationgillygize
 

More from gillygize (8)

Some Stuff I was thinking about state machines and types
Some Stuff I was thinking about state machines and typesSome Stuff I was thinking about state machines and types
Some Stuff I was thinking about state machines and types
 
Manual Layout Revisited
Manual Layout RevisitedManual Layout Revisited
Manual Layout Revisited
 
Optimize llvm
Optimize llvmOptimize llvm
Optimize llvm
 
Connecting to a REST API in iOS
Connecting to a REST API in iOSConnecting to a REST API in iOS
Connecting to a REST API in iOS
 
State ofappdevelopment
State ofappdevelopmentState ofappdevelopment
State ofappdevelopment
 
ViewController/State
ViewController/StateViewController/State
ViewController/State
 
Two-StageCreation
Two-StageCreationTwo-StageCreation
Two-StageCreation
 
Categories
CategoriesCategories
Categories
 

Recently uploaded

Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 

Recently uploaded (20)

Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 

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.