Download - Reactive cocoa 101
![Page 1: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/1.jpg)
Quiz 1
![Page 2: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/2.jpg)
Quiz 2
![Page 3: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/3.jpg)
ReactiveCocoa 101Hai Feng Kao
![Page 4: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/4.jpg)
Imperative Programming
![Page 5: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/5.jpg)
Object-Oriented Programming● 封裝、繼承、多型
![Page 6: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/6.jpg)
禪師 -鈴木俊隆
![Page 7: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/7.jpg)
Why is debugging difficult?
![Page 8: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/8.jpg)
Functional Programming● Lambda Calculus● Functions without side
effects● Programs as function
compositions (y = f(g(h(x))))
![Page 9: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/9.jpg)
Functional Reactive Programming● In functional programming, things get hard
because functions can’t have side-effectso Every time a function is called, it must
return the same y for same xo y = f(x)
● In functional reactive programming, time is an implicit parameter (no monads)
o y = f(x, t)
![Page 10: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/10.jpg)
ReactiveCocoa● A open source FRP framework
developed by Githubo iOS / OSXo MIT Licenseo 3.0 supports swifto https://github.com/ReactiveCocoa/Re
activeCocoa
![Page 11: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/11.jpg)
Signals● Primary data source of FRP● Signals send values over time until they
completeo A signal never sends anything after
completing or erringo A signal either completes, or errors
out, but never both
![Page 12: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/12.jpg)
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
valueso This is core to FRP: data transformation
of values sent over timeo y = f(g(h(x)))
![Page 13: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/13.jpg)
KVO Example
![Page 14: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/14.jpg)
Reactive Example
![Page 15: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/15.jpg)
RACSignals● always send id, not primitives
o NSInteger -> NSNumbero CGRect -> NSValue
![Page 16: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/16.jpg)
Functional Programming● The building blocks of functional world
o map, reduce, filter
![Page 17: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/17.jpg)
Map● Mapping performs a value
transformation on the given signalo It ”maps” each value sent along the
signal to a new valueo It then passes that new value along,
creating a new signal
![Page 18: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/18.jpg)
Example
![Page 19: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/19.jpg)
● Combines two or more signals’ values into one
o Useful for waiting for conditions to be met
o Useful for deciding between values
Reduce
![Page 20: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/20.jpg)
Example
![Page 21: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/21.jpg)
Map Reduce Algorithm
![Page 22: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/22.jpg)
Map Reduce Algorithm
![Page 23: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/23.jpg)
Functional Programming● The building blocks of functional world
o map, reduce, filter
![Page 24: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/24.jpg)
Filter● Filtering allows values that pass some
test to “pass through”
![Page 25: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/25.jpg)
Example
![Page 26: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/26.jpg)
Functional Programming● The building blocks of functional world
o map, reduce, filter
![Page 27: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/27.jpg)
Functional Programming● The building blocks of functional world
o map, reduce, filter
USELESS?
![Page 28: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/28.jpg)
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
![Page 29: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/29.jpg)
Example
![Page 30: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/30.jpg)
Demo
![Page 31: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/31.jpg)
Imperative vs Functional
Imperative:
Functional:
![Page 32: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/32.jpg)
Reference● Ash Furrow Reactive Cocoa 101
o https://www.youtube.com/watch?v=TlgUWYrQ0sc
● Functional Reactive Programming on iOSo https://leanpub.com/iosfrp/
![Page 33: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/33.jpg)
Next Class
![Page 34: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/34.jpg)
Next Class
![Page 35: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/35.jpg)
Homework
![Page 36: Reactive cocoa 101](https://reader030.vdocuments.mx/reader030/viewer/2022032513/55d08af7bb61ebe4188b471b/html5/thumbnails/36.jpg)
Q&A