2. Who is Nick Hodge?
• 28 years in IT, mostly as non-Developer
• Apple, Adobe, Microsoft
• Many small-scale, rapidly built applications
• Open source contributor
• Functional Reactive Programming Fan
3. Group Experiment:
Data-at-rest vs. Data-in-motion
• LINQ to IEnumerable (or IQueryable)
• “data at rest”
• yield
• What happens if data changes in midst of foreach() ?
• Reactive programming (IObservable)
• “data in motion”
• Data (or events) are pushed at you
9. Limitations of .NET Events
exchange.StockTick += (sender, args) =>
{
if (args.Quote.Symbol == “MSFT”)
{
// Imperative code
}
};
exchange.StockTick -= /* what goes here? */;
10. Observable Sequences to the Rescue
IObservable<Quote> stockQuotes = …;
var msft = stockQuotes
.Where(quote => quote.Symbol == “MSFT”);
var subscription = msft.Subscribe(quote => /* … */);
subscription.Dispose();
11. Are .NET Events Obsolete?
• .NET Events
• Code centric
• Design-time experience
• Not first class
• Non-compositional
• Lightweight
• Rigid execution model (IL)
• Observables
• Data centric
• No design-time experience
• First class objects
• Rich composition
• Slightly more cost
• Translatable with expression trees
14. Event Streams
• Towards a unified programming model
• Producers are observable sequences
• .NET events, WinRT events, sensor APIs, APM methods, tasks, etc.
• Consumers are observers
• Hooking up “continuations” or handlers
Observable
Subscribe
Observer
15. Essential Interfaces
namespace System
{
public interface IObservable<out T>
{
IDisposable Subscribe(IObserver<T> observer);
}
public interface IObserver<in T>
{
void OnNext(T value);
void OnError(Exception error);
void OnCompleted();
}
}
19. This Presentation’s Reference Presentations
• Bart De Smet “Curing Your Event Processing Blues with Reactive
Extensions (Rx)” TechEd Europe 2012
http://channel9.msdn.com/events/TechEd/Europe/2012/DEV413
• Paul Betts “Introduction to Reactive Extensions” Norwegian
Developer’s Conference 2012 http://vimeo.com/43659034
• Brendan Forster “Reactive UI – Turning MVVM up to 11”
http://vimeo.com/97329155
20. Further References, Links
• https://rx.codeplex.com/ and https://github.com/Reactive-
Extensions/Rx.NET as the starting point
• RxJS (and other languages such as Ruby, C++, Python, Java, ObjC)
https://github.com/Reactive-Extensions/RxJS
• http://www.introtorx.com/
• 101 Rx Examples
http://rxwiki.wikidot.com/101samples
• http://amzn.to/programming-rx “Programming Reactive Extensions
and LINQ”
21. Further Research, Future
• “Scalable Information Stream Processing by Bing in Support of
Cortana Scenarios” http://channel9.msdn.com/posts/Scalable-
Information-Stream-Processing-by-Bing-in-Support-of-Cortana-
Scenarios
• Actor model, “Microsoft Research project Orleans simplify
development of scalable cloud services”
http://channel9.msdn.com/Shows/Cloud+Cover/Episode-142-
Microsoft-Research-project-Orleans-simplify-development-of-
scalable-cloud-services
Notas del editor
We no longer write sync software
LINQ: the core of LINQ clojure/Haskell is a sequence (Ienumerable) things in a particular order
MoveNext() MoveNext()
Yield/return
LINQ : from sequence, and create a pipeline .Select .Where .ForEach (except last, deferred computation)
An order
LINQ describe what we are going to do with the data when get it it
… without having the data
THIS IS A MONAD
Monad : chain together functions that describe what you want to do with the data
In the C# world, events are not composable
Events are first class elements of the language
OnMouseUp + OnMouseDown != OnDoubleClick (delegates)
Timer, hook onmouseup/onmousedown, Booleans, state … erg
Events fight with different states
Event is some stuff in a particular order (time )
List; happened
Events; future or going to happen
Taking what you know about LINQ, applying to events
IEnumerable : lists
IObservable, stream of events
Idisposable, can stop the subscription early. (only if doing early, unless complete on its own) GC will cleanup
Iobservable == list, LINQ works
.Subscribe is the foreach. Nothing happens until you .Subscribe
.NET event to an observable
Special one: Subject<T> push around by hand
INotifyPropertyChanged == events