Functional Reactive Programming

52
DEV07 Functional Reactive Programming Alessandro Melchiori [email protected] - @amelchiori http://melkio.codiceplastico.com/

Transcript of Functional Reactive Programming

DEV07Functional Reactive ProgrammingAlessandro Melchiori

[email protected] - @amelchiori

http://melkio.codiceplastico.com/

Functional Reactive Programming

Functional Reactive Programming

It's declarative, stateless, side-effects free and immutable

Functional programming

It's declarative, stateless, side-effects free and immutable

Functional programming

describes what we want, not how we want

it

It's declarative, stateless, side-effects free and immutable

Functional programming

describes what we want, not how we want

it

system relys only on inputs, not external

state

It's declarative, stateless, side-effects free and immutable

Functional programming

describes what we want, not how we want

it

system relys only on inputs, not external

state

once value has been set, we can't override its

value

Return new value, instead of altering

existing ones

Give me five functions and I’ll raise up the world

[map; filter; reduce; collect; zip]

let source = [ 1; 2; 3]

let result =

List.map (fun x -> x*2) source

val result : int list = [2; 4; 6]

map

let source = [ 1; 2; 3]

let result =

List.filter (fun x -> x > 1) source

val result : int list = [2; 3]

filter

let source = [ 1; 2; 3]

let result =

List.reduce (fun acc curr -> acc + curr) source

val result : int = 6

reduce

let source = [ [ 1; 2; 3]; [ 4; 5 ] ]

let result =

List.collect (fun x -> x) source

val result : int list = [1; 2; 3; 4; 5; 6]

collect

let source1 = [ 1; 2; 3]

let source2 = [‘A’;’B’;’C’]

let result =

List.zip source1 source2

val res : (int * char) list = [(1, 'A'); (2, 'B'); (3, 'C')]

zip

Functional Reactive Programming

Functional Reactive Programming

A programming paradigm oriented around data flows

and the propagation of change.

Reactive programming

Reactive systems are responsive, resilient, elastic and message driven

Reactive manifesto (http://www.reactivemanifesto.org/)

Reactive programming

Reactive programming

1

2

sum

1diff

4

…readily responsive to a stimulus…

Reactive programming

1

1

sum

1diff

4

Reactive programming

1

1

sum

1diff

4

Reactive programming

1

1

sum

1diff

4

Reactive programming

1

1

sum

1diff

4

Reactive programming

1

1

sum

1diff

4

Reactive programming

1

1

sum

1diff

4

Reactive programming

1

1

sum

2diff

4

Reactive extensions

Rx is a library for composing asynchronous and event-based programs using observable sequences and LINQ-style query operators

(https://msdn.microsoft.com/en-us/data/gg577609.aspx)

Reactive extensions

Rx is a library for composing asynchronous and event-based programs using observable sequences and LINQ-style query operators

(https://msdn.microsoft.com/en-us/data/gg577609.aspx)

Reactive extensions

Rx is a library for composing asynchronous and event-based programs using observable sequences and LINQ-style query operators

(https://msdn.microsoft.com/en-us/data/gg577609.aspx)

Reactive extensions

Rx is a library for composing asynchronous and event-based programs using observable sequences and LINQ-style query operators

(https://msdn.microsoft.com/en-us/data/gg577609.aspx)

Reactive extensions

Rx is a library for composing asynchronous and event-based programs using observable sequences and LINQ-style query operators

(https://msdn.microsoft.com/en-us/data/gg577609.aspx)

Reactive extensions

• a set of types representing asynchronous data streams

• a set of operators to query asynchronous data streams

• a set of types to parameterize concurrency

Reactive extensions

RX = Observables + LINQ + Schedulers

demointro

Reactive extensions

Reactive extensions

publisher

Reactive extensions

publisher subscriber

Reactive extensions

publisher subscriberboth

The Observable type adds two missing semantics to the Gang of Four's Observer pattern, which are available in the Iterable type:

• The ability for the producer to signal to the consumer that there is no more data available

• The ability for the producer to signal to the consumer that an error has occurred

Reactive extensions

Reactive extensions

RX mantraeverything can be a stream

Reactive extensions: map

1 2 3 4

A B C D

Reactive extensions: filter

1 2 3 4

2 4

Reactive extensions: scan

1 2 3 4

1 3 6 10

Reactive extensions: zip

1 2 3 4

1, A 2, B 3, C 4, D

A B C D

demoClicks counter

Reactive extensions

demoAutocomplete textbox

Recap

• Functional programming

• Reactive programming

• Reactive extensions (Rx.*)

FRP is agnostic about languages. It's a mindset

Q&A

Tutto il materiale di questa sessione suhttp://www.communitydays.it/

Lascia subito il feedback su questa sessione,potrai essere estratto per i nostri premi!

Seguici suTwitter @CommunityDaysITFacebook http://facebook.com/cdaysit#CDays15