frp (rx way) · create car example: frp. 15 15 agenda motivation functional reactive programming...

30
1 FRP (Rx way) MARCH 2017

Upload: others

Post on 27-Jun-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

1

FRP (Rx way)

MARCH 2017

Page 2: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

2

2

AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING

INTRO DEMO

Page 3: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

3

3

MOORE’S LAW

Simplified version

Processor speeds, or overall processing power for computers will double every two years.

You could count on hardware improvements and wait for your app’s performance to increase

Real version

The number of transistors on an affordable CPU would double every two years

Page 4: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

44

4

WHAT HAPPENED WITH THOSE EXTRA TRANSISTORS?

Page 5: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

5

5

PARALLEL PROGRAMMING

Parallel programming is the only way to use hardware improvement to increase performance in the future!

Page 6: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

6

6

CONCURRENT PROGRAMMING

• Internet:• December 2000 … 361 millions of users … 5.8% of world population

• September 2016 .... 3675 millions of users … 50.1% of world population

• Web programming

• Communication with external devices• Sensors• Bluetooth• …

Page 7: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

77

7

MULTITHREADING IS UNAVOIDABLE

Page 8: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

8

8

SHARED MUTABLE STATES

• Some states are “invalid” and can crash or block app

• Multiple points of access => hard to control states

PROBLEM:

• Access control - setters/getters (OOP)

PREVENTION:

Page 9: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

9

9

SHARED MUTABLE STATES + MULTITHREADING

Without multithreading, bugs are 100% reproducible.• all conditions are under our control => we control execution order

With multithreading, bugs are intermittent (<< 100%)• some conditions are out of our control => execution order is unpredictable

NONDETERMINISTIC BEHAVIOUR => PROGRAMMING HELL

Page 10: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

10

10

SOLUTIONS ?

• Remove Shared Mutable States completely (???) • Functional programming

• Remove Shared Mutable States as much as possible• Remove unnecessary states

• Make const everything that can be const

• Use some Functional programming techniques (work with collections, higher order functions)

Page 11: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

11

11

CAR ASSEMBLY

Page 12: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

1212

12

CREATE CAR EXAMPLE: SINGLE-THREADED

Page 13: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

1313

13

CREATE CAR EXAMPLE: MULTI-THREADED

Page 14: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

1414

14

CREATE CAR EXAMPLE: FRP

Page 15: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

15

15

AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING

INTRO DEMO

Page 16: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

1616

16

MEET THE OBSERVABLE

Page 17: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

1717

17

MEET THE OBSERVABLE

Observable is the object that represent the stream of data

Observer can subscribe to an Observable

Operator is basically a function that accept Observable as operand, instead of the data.

Observable can send three types of signals: Value signal

Completed signal

Error signal

Page 18: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

18

18

OBSERVABLE VS ITERABLE

Iterable:

A container object capable of returning its members one at a time.

It is possible to implement all functional operators for work with enumerable collections to work with observables ! (i.e. map, zip, filter ...)

For example:

someIterable.map(someFunction) -> newIterable

someObservable.map(someFunction) -> newObservable

Page 19: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

1919

19

OBSERVABLE VS ITERABLE

• At one point of time ALL of the Iterable elements are accessible

• Only one element of the Observable is accessible at the moment of its creation

• Iterable have a pull based access

• Observable have a push based access

• It is possible to convert from Observable to Iterable and vice versa

Page 20: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

2020

20

MAP OPERATOR

Page 21: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

21

21

CALLBACK VS OBSERVABLE (BRIDGING)

void myAsyncFunction(Data someData, Callback<MyType> myCallback);

Observable<MyType> myFrpAsyncFunction(Data someData);

Page 22: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

22

22

AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING

INTRO DEMO

Page 23: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

23

23

DEMO: DOWNLOAD IMAGE

Page 24: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

24

24

IMPERATIVE VS. DECLARATIVE

Page 25: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

25

25

IMPERATIVE VS. DECLARATIVE

Page 26: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

26

26

DEMO: RGB SLIDERS (PART 1)

Page 27: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

27

27

DEMO: RGB SLIDERS (PART 2)

Page 28: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

28

28

DEMO: RGB SLIDERS (PART 3)

Page 29: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

29

29

EXTERNAL LINKS

http://reactivex.io/

http://rxmarbles.com/

Page 30: FRP (Rx way) · CREATE CAR EXAMPLE: FRP. 15 15 AGENDA MOTIVATION FUNCTIONAL REACTIVE PROGRAMMING INTRO DEMO. 16 16 MEET THE OBSERVABLE. 17 17 MEET THE OBSERVABLE Observable is the

30

SLOBODAN PAVICANDROID DEVELOPER

RASKO GOJKOVICSENIOR IOS DEVELOPER