The document discusses reactive programming and frameworks. It introduces reactive programming as a way to think about asynchronous and event-based programming that is fundamental to cloud, web and mobile applications. It shows how asynchronous and event-based computations can be viewed as push-based collections by dualizing enumerable collections to observable collections. This allows applying LINQ-style queries to asynchronous programming. Examples are provided for moving a ball with keyboard input and dragging the mouse to draw, implemented imperatively and declaratively with observables.
16. 1
Observer
Subscribe(o)
2
OnNext(e)
3
OnCompleted()
Producer pushes
successive elements X
from the collection
17. IEnumerable & IEnumerator are
prototypical interfaces for interactive
collections and interactive programs.
IObservable & IObserver are
prototypical interfaces for observable
collections and reactive, asynchronous
& event-based programs.
21. LINQ to Observable
If you are writing
LINQ or declarative code
in an interactive program...
22. LINQ to Observable
If you are writing
LINQ or declarative code
in an interactive program...
You already know how to use it!
23. ... the principle we go by is, don't
expect to see a particular
concurrency model put into C#
because there're many different
concurrency model ... it's more
about finding things are common
to to all kinds of concurrency ...
- Anders Hejlsberg
24. ... the principle we go by is, don't
expect to see a particular
concurrency model put into C#
because there're many different
concurrency model ... it's more
about finding things are common
to to all kinds of concurrency ...
- Anders Hejlsberg
25. LINQ to Object
Standard query operators
Select
Where
SelectMany
...
Extended query operators
Zip
Aggregate
...
26. LINQ to Observable
Standard query operators
Select
Where
SelectMany
...
Extended query operators
Zip
Throttle
...
29. Declarative Impl.
//
filter
the
KeyPress
events
when
playing
var
keyPress
=
GetKeyPress().Where(_
=>
isPlaying);
//
filter
the
events
to
move
left
var
moveLeft
=
from
ev
in
keyPress
where
ev.EventArgs.KeyChar
==
'a'
where
ball.Left
>
0
select
ev;
moveLeft.Subscribe(_
=>
ball.Left
-‐=
5);
//
filter
the
events
to
move
top
var
moveTop
=
from
ev
in
keyPress
where
ev.EventArgs.KeyChar
==
'w'
where
ball.Top
>
0
select
ev;
moveTop.Subscribe(_
=>
ball.Top
-‐=
5);
31. var
mouseMove
=
GetMouseMove();
var
mouseDiff
=
mouseMove.Zip(mouseMove.Skip(1),
(prev,
curr)
=>
new
{
PrevPos
=
new
Point(
prev.EventArgs.X,
prev.EventArgs.Y),
CurrPos
=
new
Point(
curr.EventArgs.X,
curr.EventArgs.Y)
});
var
mouseDrag
=
from
_
in
GetMouseDown()
from
diff
in
mouseDiff.TakeUntil(GetMouseUp())
select
diff;
mouseDrag.Subscribe(d
=>
DrawLine(d.PrevPos,
d.CurrPos));
32. Everything in Web is
Asynchronous
User actions
AJAX requests
Animations
...
33. Reactive Framework in
JavaScript
A full featured port for JavaScript
Easy-to-use conversions from existing DOM,
XmlHttpRequest, etc
In a download size of less than 7kb (gzipped)
Bindings for various libraries / frameworks
jQuery
MooTools
Dojo
...
39. var
container
=
$("#container");
var
mouseMove
=
container.toObservable("mousemove");
for
(var
i
=
0;
i
<
text.length;
i++)
{
(function(i)
{
var
ele
=
$("<span/>").text(text.charAt(i));
ele.css({position:
"absolute"}).appendTo(container);
mouseMove.Delay(i
*
100).Subscribe(function
(ev)
{
ele.css({
left:
ev.clientX
+
i
*
20
+
15
+
"px",
top:
ev.clientY
+
"px"
});
});
})(i);
40. Benefits of Rx
Easy to composite and coordinate async
operations
Easy to express the algorithm (code locality)
Easy to be unit tested
...
41. Rx & Language Features
Features in C# that Rx uses
Extension method
Lambda expression & closure
Type inference
LINQ query expression
Rx has been implemented in ...
C# & VB
JavaScript
F#
42. Port to other Languages
Rx can be easily ported to various languages
Scala
Ruby
Python
modern languages with basic functional features
Almost impossible to implement Rx in Java
Cannot extend a type without breaking code
Missing enough functional features
43. Resources
Matthew Podwysocki
http://codebetter.com/blogs/matthew.podwysocki/
Reactive Framework on MSDN DevLabs
http://msdn.microsoft.com/en-us/devlabs/
ee794896.aspx
Tomáš Petříček
http://tomasp.net/