Reactive programming

1,508 views

Published on

Training given to FirebootCamp attendees 15th August 2014 on Reactive Programming.

Published in: Software
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,508
On SlideShare
0
From Embeds
0
Number of Embeds
44
Actions
Shares
0
Downloads
7
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • 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
  • Reactive programming

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

    ×