KEMBAR78
Reactive programming | PPTX
Reactive Programming
Nick Hodge
Developer, Geek
nhodge@mungr.com
http://nickhodge.com/
https://gist.github.com/nickhodge for codesnippets
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
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
Reactive Programming
demo required
Reactive Programming vs. TPL
• Task Parallel Library (TPL) http://msdn.microsoft.com/en-
us/concurrency
• PLINQ
The core of LINQ
is the sequence
A sequence is just
some stuff in a particular order
What about Rx?
Let’s talk about events
Limitations of .NET Events
exchange.StockTick += (sender, args) =>
{
if (args.Quote.Symbol == “MSFT”)
{
// Imperative code
}
};
exchange.StockTick -= /* what goes here? */;
Observable Sequences to the Rescue
IObservable<Quote> stockQuotes = …;
var msft = stockQuotes
.Where(quote => quote.Symbol == “MSFT”);
var subscription = msft.Subscribe(quote => /* … */);
subscription.Dispose();
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
The Event Processing Landscape
Social
media
RSS feeds
GPS
Server management
Reactive Extensions Architecture
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
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();
}
}
Observable Sources
• Single items / empty Observable.Return()
• Lists and arrays
• UI Events
• Async methods
• Subjects (demo)
• Property Changes
Cold / Hot
• Hot : example MouseMove
• Cold: when you subscribe, something happens (it waits)
UI Example, Windows 8 Universal App
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
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”
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

Reactive programming

  • 1.
    Reactive Programming Nick Hodge Developer,Geek nhodge@mungr.com http://nickhodge.com/ https://gist.github.com/nickhodge for codesnippets
  • 2.
    Who is NickHodge? • 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
  • 4.
  • 5.
    Reactive Programming vs.TPL • Task Parallel Library (TPL) http://msdn.microsoft.com/en- us/concurrency • PLINQ
  • 6.
    The core ofLINQ is the sequence
  • 7.
    A sequence isjust some stuff in a particular order
  • 8.
    What about Rx? Let’stalk about events
  • 9.
    Limitations of .NETEvents exchange.StockTick += (sender, args) => { if (args.Quote.Symbol == “MSFT”) { // Imperative code } }; exchange.StockTick -= /* what goes here? */;
  • 10.
    Observable Sequences tothe Rescue IObservable<Quote> stockQuotes = …; var msft = stockQuotes .Where(quote => quote.Symbol == “MSFT”); var subscription = msft.Subscribe(quote => /* … */); subscription.Dispose();
  • 11.
    Are .NET EventsObsolete? • .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
  • 12.
    The Event ProcessingLandscape Social media RSS feeds GPS Server management
  • 13.
  • 14.
    Event Streams • Towardsa 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 { publicinterface IObservable<out T> { IDisposable Subscribe(IObserver<T> observer); } public interface IObserver<in T> { void OnNext(T value); void OnError(Exception error); void OnCompleted(); } }
  • 16.
    Observable Sources • Singleitems / empty Observable.Return() • Lists and arrays • UI Events • Async methods • Subjects (demo) • Property Changes
  • 17.
    Cold / Hot •Hot : example MouseMove • Cold: when you subscribe, something happens (it waits)
  • 18.
    UI Example, Windows8 Universal App
  • 19.
    This Presentation’s ReferencePresentations • 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

Editor's Notes

  • #7 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)
  • #8 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
  • #9 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
  • #16 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
  • #17 .NET event to an observable Special one: Subject<T> push around by hand INotifyPropertyChanged == events