72
LET IT FL O W Introduction to Functional Reactive Programming @ArturSkowronski

Let it Flow - Introduction to Functional Reactive Programming

Embed Size (px)

DESCRIPTION

Introduction to Functional Reactiv Programming presented during meet.js Summit in Poznań 27.09.2014

Citation preview

Page 1: Let it Flow - Introduction to Functional Reactive Programming

LET IT FLOWIntroduction to Functional Reactive Programming

@ArturSkowronski

Page 2: Let it Flow - Introduction to Functional Reactive Programming

About Me

@ArturSkowronski

Page 3: Let it Flow - Introduction to Functional Reactive Programming
Page 4: Let it Flow - Introduction to Functional Reactive Programming
Page 5: Let it Flow - Introduction to Functional Reactive Programming
Page 6: Let it Flow - Introduction to Functional Reactive Programming

REACTIVE PROGRAMMINGFunctional

Page 7: Let it Flow - Introduction to Functional Reactive Programming

DATAFLOW

Page 8: Let it Flow - Introduction to Functional Reactive Programming

PROPAGATION OF CHANGE

Page 9: Let it Flow - Introduction to Functional Reactive Programming

a = 1 b = 2

c = a + b

Page 10: Let it Flow - Introduction to Functional Reactive Programming

b = 3

c = ?

Page 11: Let it Flow - Introduction to Functional Reactive Programming

C

A

B

Page 12: Let it Flow - Introduction to Functional Reactive Programming

Properties or

Signals

or

Behaviours

Page 13: Let it Flow - Introduction to Functional Reactive Programming
Page 14: Let it Flow - Introduction to Functional Reactive Programming

Event Streams

Page 15: Let it Flow - Introduction to Functional Reactive Programming

Single Event

Page 16: Let it Flow - Introduction to Functional Reactive Programming

Events Stream

Page 17: Let it Flow - Introduction to Functional Reactive Programming
Page 18: Let it Flow - Introduction to Functional Reactive Programming
Page 19: Let it Flow - Introduction to Functional Reactive Programming
Page 20: Let it Flow - Introduction to Functional Reactive Programming

Dirty Checking!

Change Listeners!

Page 21: Let it Flow - Introduction to Functional Reactive Programming

Creator: Juha Paananen

Page 22: Let it Flow - Introduction to Functional Reactive Programming
Page 23: Let it Flow - Introduction to Functional Reactive Programming
Page 24: Let it Flow - Introduction to Functional Reactive Programming

EventStreamsProperties

Observable

Page 25: Let it Flow - Introduction to Functional Reactive Programming

Talk is cheap. Show me the codeLinus Torvald

Page 26: Let it Flow - Introduction to Functional Reactive Programming

Examples

Page 27: Let it Flow - Introduction to Functional Reactive Programming

Example 1: ScoreBoard

Page 28: Let it Flow - Introduction to Functional Reactive Programming

var score = 0

$('#plus').click(function(){ score = score + 1; $('#result').html(score); })

$('#minus').click(function(){ score = score - 1; $('#result').html(score); })

Page 29: Let it Flow - Introduction to Functional Reactive Programming

var plus = $('#plus') .asEventStream('click'); !

var minus = $('#minus') .asEventStream('click');

Page 30: Let it Flow - Introduction to Functional Reactive Programming
Page 31: Let it Flow - Introduction to Functional Reactive Programming

var score = plus.map(1)

.merge(minus.map(-1))

.scan(0, function(x, y){ return x + y })

Page 32: Let it Flow - Introduction to Functional Reactive Programming

Map

Page 33: Let it Flow - Introduction to Functional Reactive Programming

var score = plus.map(1)

.merge(minus.map(-1))

.scan(0, function(x, y){ return x + y })

Page 34: Let it Flow - Introduction to Functional Reactive Programming

var

.merge(minus.map(-1))

.

Page 35: Let it Flow - Introduction to Functional Reactive Programming

a

b

a.merge(b)

1 3

2

1 2 3

Merge

Page 36: Let it Flow - Introduction to Functional Reactive Programming
Page 37: Let it Flow - Introduction to Functional Reactive Programming

var

.

.scan(0, function(x, y){ return x + y })

Page 38: Let it Flow - Introduction to Functional Reactive Programming

Event Stream

Property

Page 39: Let it Flow - Introduction to Functional Reactive Programming

a

a.scan('',sum)

"a"

"" "c" "ca"

"c" "t"

"cat"

Seed Value "" + "c"

"c" + "a"

"ca" + "t"

Scan

Page 40: Let it Flow - Introduction to Functional Reactive Programming

score.onValue(function(value){ $("#score").html(value); });

Side Effect

Page 41: Let it Flow - Introduction to Functional Reactive Programming
Page 42: Let it Flow - Introduction to Functional Reactive Programming

var favorite = $('#favorite') .asEventStream('click') .map(2);

Page 43: Let it Flow - Introduction to Functional Reactive Programming

var score = up .merge(down) .merge(favorite) .scan(0, sum)

Page 44: Let it Flow - Introduction to Functional Reactive Programming
Page 45: Let it Flow - Introduction to Functional Reactive Programming

Example 1I: Combining DataStreams

Page 46: Let it Flow - Introduction to Functional Reactive Programming

var username =

$('#username').asEventStream('keyup')

.map(inputVal)

function inputVal(ev) {

return $(ev.currentTarget).val() }

Username:

Page 47: Let it Flow - Introduction to Functional Reactive Programming

var password =

$('#password').asEventStream('keyup')

.map(inputVal)

Username:

Password:

Page 48: Let it Flow - Introduction to Functional Reactive Programming
Page 49: Let it Flow - Introduction to Functional Reactive Programming

uValid = username.map(isValid) !

!

pValid = password.map(isValid)

Custom

Funct

ion

Page 50: Let it Flow - Introduction to Functional Reactive Programming

uValid.combine(pValid, function(a, b){

return a && b

})

validForm

=

Page 51: Let it Flow - Introduction to Functional Reactive Programming

a

b

a.combine (b, plus)

1 3

2

3 5

Combine

Page 52: Let it Flow - Introduction to Functional Reactive Programming

validForm.onValue(function(value) {

$("#submit").attr("disabled", !enabled)

})

Submit

Page 53: Let it Flow - Introduction to Functional Reactive Programming
Page 54: Let it Flow - Introduction to Functional Reactive Programming

Example III: Promises

Page 55: Let it Flow - Introduction to Functional Reactive Programming

Bacon.fromPromise

var ajaxCall = Bacon.fromPromise( $.ajax({ url : "meetjs/users.json"}) )

var ajaxCall(query){ return Bacon.fromPromise( $.ajax({ url : "meetjs/?q=" + query})) }

Page 56: Let it Flow - Introduction to Functional Reactive Programming

Search:

var search =

$('#search').asEventStream('keyup')

.map(inputVal)

search.map(ajaxCall).onValue(function(value){

value.onValue(function(value){

doSomethingWithResult(value);

});

});

Page 57: Let it Flow - Introduction to Functional Reactive Programming
Page 58: Let it Flow - Introduction to Functional Reactive Programming

Search:

search.map(ajaxCall).onValue(function(value){

value.onValue(function(value){

doSomethingWithResult(value);

});

});

Page 59: Let it Flow - Introduction to Functional Reactive Programming
Page 60: Let it Flow - Introduction to Functional Reactive Programming

flatMap

Page 61: Let it Flow - Introduction to Functional Reactive Programming

a

f(1)

f(2)

~1 ~2

FlatMapa.flatMap(f) "a" "b"

"a"

"b"

Page 62: Let it Flow - Introduction to Functional Reactive Programming

a

f(1)

f(2)

~1 ~2

FlatMapLatest

a.flatMapLatest(f) "a"

"a"

"b"

Page 63: Let it Flow - Introduction to Functional Reactive Programming

search.flatMapLatest(ajaxCall)

.onValue(function(value){

doSomethingWithResult(value);

})

Page 64: Let it Flow - Introduction to Functional Reactive Programming

WHERE ?

Page 65: Let it Flow - Introduction to Functional Reactive Programming
Page 66: Let it Flow - Introduction to Functional Reactive Programming
Page 67: Let it Flow - Introduction to Functional Reactive Programming
Page 68: Let it Flow - Introduction to Functional Reactive Programming

Wrap Up

Page 69: Let it Flow - Introduction to Functional Reactive Programming

Filters

Bus

Combine Templates

This Presentation

Page 70: Let it Flow - Introduction to Functional Reactive Programming
Page 71: Let it Flow - Introduction to Functional Reactive Programming
Page 72: Let it Flow - Introduction to Functional Reactive Programming

Questions?

Thank You!

@ArturSkowronski arturskowronski.com