Difference between revisions of "Reactive functional programming"
From Wiki2
(→rxjs) |
|||
Line 31: | Line 31: | ||
---0---1---2---3(8,9,10,11|) | ---0---1---2---3(8,9,10,11|) | ||
(a|) foo | |||
----0----1----2 bar | |||
bar.startWith(foo) | |||
other combination: 'or' style | |||
---0---1---2---4 | |||
-a-b-c-d-e-f- | |||
merge | |||
-a-(0b)-c-(1d)-e-(2f)---4 | |||
'and' style | |||
---0---1---2---4 | |||
-5-6-7---8--- | |||
foo.combineLatest(bar, (x,y)=>x+y) | |||
---5,6,7-8-9-10--12 | |||
---H---e---l---l | |||
--0-1---0-1-0-- | |||
foo.withLatestFrom(bar, (c,n)=>n==1?..toUpperCase():c.toLowerCase()) | |||
---h---E---L--l | |||
zip | |||
first+first, second+second | |||
----0----2---4----5| | |||
-5-6-7-8 | |||
foo.zip(bar, (x+y)=>x+y) | |||
----5----8---1----13 | |||
o mo better... | |||
(hekk|) | |||
---0---1---2---3 | |||
foo.zip(bar, (x+y)=>x) | |||
---h---e---k---k | |||
transformation | |||
accumulates horizantally | |||
scan | |||
----h---e---d---f-- | |||
scan(acc,x,'d') | |||
d--(dh)-(dhe)-(dhef) | |||
to add up clicks | |||
---x----x--x | |||
clicks.map((x)=>1) | |||
---1----1--1- | |||
.scan((accc,x)=>acc+x,0) | |||
0--1----2--3 | |||
buffer | |||
----h----e----l---l----o (foo) | |||
foo.bufferCount(2) | |||
---------he-------ll---[o] | |||
or | |||
foo.bufferTime(900ms) | |||
-------h-------el-------[o] | |||
-----------1---1---------1 (bar) | |||
foo.buffer(bar) | |||
-----------he--l---------lo | |||
delay | |||
0---1---2---3---4 | |||
foo.delay(tommorrow) | |||
-------------0---1---2---3---4 | |||
[https://www.youtube.com/watch?v=XRYN2xt11Ek netflix reactive extensions] | [https://www.youtube.com/watch?v=XRYN2xt11Ek netflix reactive extensions] | ||
Revision as of 10:02, 25 May 2016
rxjs
internally:
function map(fn) { var source=this result = Rx.Observable.create(function subscribe(observer){ source.subscribe( function(x) {observer.next(fn(x));}, function(err){ oberver.error(err));}, function() {observer.complete();} ) }) } Rx.Observable.prototype.map = map
operators
Rx.Observable.of(4,5,6,7,8) (45678|)
Rx.Observable.interval(500).take(4), a kind of filter, also: first, takeLast, last, skip, skipLast ---0---1---2---3|
[4,5,6,7].do(x=>console.log('dog is '+x)) //doesn't touch the Observable, passes it through
Rx.Observable.interval(500).take(4) ---0---1---2---3| foo (8,9,10,11|) more foo.concat(more) ---0---1---2---3(8,9,10,11|)
(a|) foo ----0----1----2 bar bar.startWith(foo)
other combination: 'or' style
---0---1---2---4 -a-b-c-d-e-f- merge -a-(0b)-c-(1d)-e-(2f)---4
'and' style
---0---1---2---4 -5-6-7---8--- foo.combineLatest(bar, (x,y)=>x+y) ---5,6,7-8-9-10--12 ---H---e---l---l --0-1---0-1-0-- foo.withLatestFrom(bar, (c,n)=>n==1?..toUpperCase():c.toLowerCase()) ---h---E---L--l
zip first+first, second+second
----0----2---4----5| -5-6-7-8 foo.zip(bar, (x+y)=>x+y) ----5----8---1----13
o mo better... (hekk|) ---0---1---2---3 foo.zip(bar, (x+y)=>x) ---h---e---k---k
transformation
accumulates horizantally scan ----h---e---d---f-- scan(acc,x,'d') d--(dh)-(dhe)-(dhef)
to add up clicks ---x----x--x clicks.map((x)=>1) ---1----1--1- .scan((accc,x)=>acc+x,0) 0--1----2--3
buffer ----h----e----l---l----o (foo) foo.bufferCount(2) ---------he-------ll---[o] or foo.bufferTime(900ms) -------h-------el-------[o]
-----------1---1---------1 (bar) foo.buffer(bar) -----------he--l---------lo
delay 0---1---2---3---4 foo.delay(tommorrow) -------------0---1---2---3---4