13. Code Locality is Broken
• Used to expressing algorithms linearly
• Async requires logical division of algorithms
• No if / using / while / for ...
• Very difficult to
• Combine multiple asynchronous operations
• Deal with exceptions and cancellation
21. “yield return” for Async
• Coming with new programming patterns
• Keep code locality
• Good parts: support if / using / while / for ...
• But not perfect: cannot use try...catch
• The primitives for Fibers - lightweight
computation units
23. F#
• Language by Don Syme, MS Research
• Strongly statically typed language
• Functional language with OO ability
• For industry and education
• Open source (Apache 2.0)
• Cross-platform supported by Microsoft
25. What’s async { ... }
... 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
28. Async Workflow
React!
async {
let! res = <async work>
...
}
an HTTP Response
an UI Event
a Timer Callback
a Query Response
a Web Servcie Response
a Disk I/O Completion
an Agent Message
32. F# Async Workflow
• Library, not a language feature
• Based on Computation Expressions in F#
• Support all kinds of language constructions
• Error handling: try...catch
• Loop: while / for (like “foreach” in C#)
• Others: if / use (like “using” in C#), etc.
• Easy to
• Combine multiple asynchronous operations
• Deal with exceptions and cancellation
33. F# Resources
http://fsharp.net
Programming F# Expert F# 2.0 Real World FP
41. 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.
42. LINQ to Observable
If you are writing
LINQ or declarative code
in an interactive program...
43. LINQ to Observable
If you are writing
LINQ or declarative code
in an interactive program...
You already know how to use it!
44. Again
... 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
45. Again
... 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
46. Rx 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
• ...
47. Time Flies like an Arrow
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);
49. Benefits of Rx
• Easy to composite and coordinate async
operations
• Express the algorithm in functional ways
• Helper method: For / While / If / Try / Switch...
• Easy to be unit tested
• ...
50. 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#
51. Portability
• 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
52. Rx 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/
53. Comparison
• F# Async Workflow
• Elegant, simple, easy to use
• Can only be used at server-side (WebSharper come to
rescure?)
• Reactive Framework
• Can be used at both server-side and client-side.
• New async model brings learning cost.
57. Source
async Task<XElement> GetRssAsync(string url) {
var client = new WebClient();
var task = client.DownloadStringTaskAsync(url);
var text = await task;
var xml = XElement.Parse(text);
return xml;
}
58. Compiled
Task<XElement> GetRssAsync(string url) {
var $builder = AsyncMethodBuilder<XElement>.Create();
var $state = 0;
TaskAwaiter<string> $a1;
Action $resume = delegate {
try {
if ($state == 1) goto L1;
var client = new WebClient();
var task = client.DownloadStringTaskAsync(url);
$state = 1;
$a1 = task.GetAwaiter();
if ($a1.BeginAwait($resume)) return;
L1: var text = $a1.EndAwait();
var xml = XElement.Parse(text);
$builder.SetResult(xml);
}
catch (Exception $ex) { $builder.SetException($ex); }
};
$resume();
return $builder.Task;
}
59. Conclusion
• Async Programming is difficult
• New programming language / feature /
library / model can help
• JavaScript is incredible.