reactive cocoa 101

36
Quiz 1

Upload: hai-feng-kao

Post on 16-Aug-2015

164 views

Category:

Mobile


0 download

TRANSCRIPT

Page 1: Reactive cocoa 101

Quiz 1

Page 2: Reactive cocoa 101

Quiz 2

Page 3: Reactive cocoa 101

ReactiveCocoa 101Hai Feng Kao

Page 4: Reactive cocoa 101

Imperative Programming

Page 5: Reactive cocoa 101

Object-Oriented Programming● 封裝、繼承、多型

Page 6: Reactive cocoa 101

禪師 -鈴木俊隆

Page 7: Reactive cocoa 101

Why is debugging difficult?

Page 8: Reactive cocoa 101

Functional Programming● Lambda Calculus● Functions without side

effects● Programs as function

compositions (y = f(g(h(x))))

Page 9: Reactive cocoa 101

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

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

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

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

KVO Example

Page 14: Reactive cocoa 101

Reactive Example

Page 15: Reactive cocoa 101

RACSignals● always send id, not primitives

o NSInteger -> NSNumbero CGRect -> NSValue

Page 16: Reactive cocoa 101

Functional Programming● The building blocks of functional world

o map, reduce, filter

Page 17: Reactive cocoa 101

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

Example

Page 19: Reactive cocoa 101

● 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

Example

Page 21: Reactive cocoa 101

Map Reduce Algorithm

Page 22: Reactive cocoa 101

Map Reduce Algorithm

Page 23: Reactive cocoa 101

Functional Programming● The building blocks of functional world

o map, reduce, filter

Page 24: Reactive cocoa 101

Filter● Filtering allows values that pass some

test to “pass through”

Page 25: Reactive cocoa 101

Example

Page 26: Reactive cocoa 101

Functional Programming● The building blocks of functional world

o map, reduce, filter

Page 27: Reactive cocoa 101

Functional Programming● The building blocks of functional world

o map, reduce, filter

USELESS?

Page 28: Reactive cocoa 101

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

Example

Page 30: Reactive cocoa 101

Demo

Page 31: Reactive cocoa 101

Imperative vs Functional

Imperative:

Functional:

Page 32: Reactive cocoa 101

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

Next Class

Page 34: Reactive cocoa 101

Next Class

Page 35: Reactive cocoa 101

Homework

Page 36: Reactive cocoa 101

Q&A