reactive cocoa 101

Post on 16-Aug-2015

164 Views

Category:

Mobile

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Quiz 1

Quiz 2

ReactiveCocoa 101Hai 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-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)

ReactiveCocoa● A open source FRP framework

developed by Githubo iOS / OSXo MIT Licenseo 3.0 supports swifto https://github.com/ReactiveCocoa/Re

activeCocoa

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

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)))

KVO Example

Reactive Example

RACSignals● always send id, not primitives

o NSInteger -> NSNumbero CGRect -> NSValue

Functional Programming● The building blocks of functional world

o map, reduce, filter

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

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=TlgUWYrQ0sc

● Functional Reactive Programming on iOSo https://leanpub.com/iosfrp/

Next Class

Next Class

Homework

Q&A

top related