Quiz 1
1
Quiz 2
ReactiveCocoa 101
Hai Feng Kao
Imperative Programming
Object-Oriented Programming
● 封裝、繼承、多型
禪師-鈴木俊隆
Why is debugging difficult?
Functional Programming
● Lambda Calculus
● Functions without side
effects
● Programs as function
compositions (y =
f(g(h(x))))
Functional Reactive Programming
● In functional programming, things get
hard because functions can’t have side-
effects
o Every time a function is called, it must
return the same y for same x
o y = f(x)
● In functional reactive programming, time
is an implicit parameter (no monads)
o y = f(x, t)
ReactiveCocoa
● A open source FRP framework
developed by Github
o iOS / OSX
o MIT License
o 3.0 supports swift
o https://github.com/ReactiveCocoa/Re
activeCocoa
Signals
● Primary data source of FRP
● Signals send values over time until they
complete
o A signal never sends anything after
completing or erring
o A signal either completes, or errors
out, but never both
Signals(cont.)
● A signal has no concept of a “current
value” or “past value”
● It’s like a pipe
● Signals can be chained to transform
the values
o This is core to FRP: data
transformation of values sent over
time
KVO Example
Reactive Example
RACSignals
● always send id, not primitives
o NSInteger -> NSNumber
o CGRect -> NSValue
Functional Programming
● The building blocks of functional world
o map, reduce, filter
Map
● Mapping performs a value
transformation on the given signal
o It ”maps” each value sent along the
signal to a new value
o It then passes that new value along,
creating a new signal
Example
● Combines two or more signals’ values
into one
o Useful for waiting for conditions to be
met
o Useful for deciding between values
Reduce
Example
Map Reduce Algorithm
Map Reduce Algorithm
Functional Programming
● The building blocks of functional world
o map, reduce, filter
Filter
● Filtering allows values that pass some
test to “pass through”
Example
Functional Programming
● The building blocks of functional world
o map, reduce, filter
Functional Programming
● The building blocks of functional world
o map, reduce, filter
USELESS?
Bindings
● Bindings can bind a property to the
latest value sent on a signal
● All bindings created with the RAC
macro are one-way
● Bindings are the secret sauce that hold
apps together
Example
Demo
Imperative vs Functional
Imperative:
Functional:
Reference
● Ash Furrow Reactive Cocoa 101
o https://www.youtube.com/watch?v=Tl
gUWYrQ0sc
● Functional Reactive Programming on iOS
o https://leanpub.com/iosfrp/
Next Class
Next Class
Homework
Q&A

Reactive cocoa 101