rx curing your asynchronous programming blues
play

Rx: Curing your Asynchronous Programming Blues Bart J.F. - PowerPoint PPT Presentation

Rx: Curing your Asynchronous Programming Blues Bart J.F. De Smet Microso' Corpora,on bartde@microso'.com Why Should I Care? GPS RSS


  1. Rx: ¡Curing ¡your ¡Asynchronous ¡ Programming ¡Blues ¡ Bart ¡J.F. ¡De ¡Smet ¡ Microso' ¡Corpora,on ¡ bartde@microso'.com ¡

  2. Why ¡Should ¡I ¡Care? ¡ GPS ¡ RSS ¡ ¡ ¡ ¡ ¡ ¡feeds ¡ Stock ¡,ckers ¡ Social ¡ media ¡ Server ¡management ¡

  3. Mission ¡Statement ¡ Too ¡hard ¡ today! ¡ ( ! ¡ ◦ ¡ " )( # ) ¡= ¡ ! ( " ( # )) Rx is a library for composing asynchronous and event-based programs using observable sequences. Queries? ¡ LINQ? ¡ .NET ¡3.5 ¡SP1 ¡and ¡4.0 ¡ • Silverlight ¡3 ¡and ¡4 ¡ • XNA ¡3.1 ¡for ¡XBOX ¡and ¡Zune ¡ • Windows ¡Phone ¡7 ¡ • JavaScript ¡(RxJS) ¡ • MSDN ¡Data ¡Developer ¡Center ¡ NuGet ¡

  4. Essen,al ¡Interfaces ¡ interface ¡IEnumerable<out ¡T> ¡ { ¡ ¡ ¡ ¡ ¡IEnumerator<T> ¡ Get Enumerator(); ¡ } ¡ C# 4.0 covariance interface ¡IEnumerator<out ¡T> ¡: ¡IDisposable ¡ { ¡ You could get ¡ ¡ ¡ ¡bool ¡ ¡ ¡ Move Next(); ¡ stuck ¡ ¡ ¡ ¡T ¡ ¡ ¡ ¡ ¡ ¡Current ¡{ ¡get; ¡} ¡ ¡ ¡ ¡ ¡void ¡ ¡ ¡Reset(); ¡ } ¡

  5. Essen,al ¡Interfaces ¡ (WaiHng ¡to ¡move ¡next) ¡ C# 4.0 covariance moving ¡on ¡ You could get stuck

  6. Mathema,cal ¡Duality ¡ Because ¡the ¡Dutch ¡are ¡(said ¡to ¡be) ¡cheap ¡ – Electricity : ¡ ¡inductor ¡and ¡capacitor ¡ – Logic : ¡ ¡De ¡Morgan’s ¡Law ¡ ¬ (% ∧ &) ≡¬ % ∨¬ & ¡ ¡ ¬ (% ∨ &) ≡¬ % ∧¬ & ¡ ¡ – Programming ? ¡

  7. What’s ¡the ¡dual ¡of ¡IEnumerable? ¡ The ¡recipe ¡to ¡dualiza,on ¡ h^p://en.wikipedia.org/wiki/ Dual_(category_theory) Reversing arrows … Input becomes output and vice versa Making ¡a ¡U-­‑turn ¡ in ¡synchrony ¡

  8. What’s ¡the ¡dual ¡of ¡IEnumerable? ¡ interface ¡IEnumerable<out ¡T> ¡ { ¡ ¡ ¡IEnumerator<T> ¡GetEnumerator(); ¡ } ¡ ¡ interface ¡IEnumerator<out ¡T> ¡: ¡IDisposable ¡ { ¡ ¡ ¡bool ¡ ¡MoveNext(); ¡ ¡ ¡T ¡ ¡ ¡ ¡ ¡Current ¡{ ¡get; ¡} ¡ Properties ¡ ¡void ¡ ¡Reset(); ¡ are methods } ¡

  9. What’s ¡the ¡dual ¡of ¡IEnumerable? ¡ interface ¡IEnumerable<out ¡T> ¡ { ¡ ¡ ¡IEnumerator<T> ¡GetEnumerator(); ¡ } ¡ ¡ interface ¡IEnumerator<out ¡T> ¡: ¡IDisposable ¡ { ¡ ¡ ¡bool ¡ ¡MoveNext(); ¡ ¡ ¡T ¡ ¡ ¡ ¡ ¡GetCurrent(); ¡ ¡ ¡void ¡ ¡Reset(); ¡ A historical mishap } ¡

  10. What’s ¡the ¡dual ¡of ¡IEnumerable? ¡ interface ¡IEnumerable<out ¡T> ¡ { ¡ ¡ ¡IEnumerator<T> ¡GetEnumerator(); ¡ } ¡ ¡ interface ¡IEnumerator<out ¡T> ¡: ¡IDisposable ¡ { ¡ No checked ¡ ¡bool ¡ ¡MoveNext(); ¡ exceptions in .NET / ¡ ¡T ¡ ¡ ¡ ¡ ¡GetCurrent(); ¡ C# } ¡

  11. What’s ¡the ¡dual ¡of ¡IEnumerable? ¡ interface ¡IEnumerable<out ¡T> ¡ { ¡ ¡ ¡IEnumerator<T> ¡GetEnumerator(); ¡ } ¡ ¡ interface ¡IEnumerator<out ¡T> ¡: ¡IDisposable ¡ { ¡ ¡ ¡bool ¡ ¡MoveNext() ¡throws ¡Exception; ¡ ¡ ¡T ¡ ¡ ¡ ¡ ¡GetCurrent(); ¡ } ¡

  12. What’s ¡the ¡dual ¡of ¡IEnumerable? ¡ interface ¡IEnumerable<out ¡T> ¡ { ¡ ¡ ¡IEnumerator<T> ¡GetEnumerator(); ¡ } ¡ ¡ interface ¡IEnumerator<out ¡T> ¡: ¡IDisposable ¡ { ¡ ¡ ¡bool ¡ ¡MoveNext() ¡throws ¡Exception; ¡ ¡ ¡T ¡ ¡ ¡ ¡ ¡GetCurrent(); ¡ This is an } ¡ output too !

  13. What’s ¡the ¡dual ¡of ¡IEnumerable? ¡ interface ¡IEnumerable<out ¡T> ¡ { ¡ ¡ ¡IEnumerator<T> ¡GetEnumerator(); ¡ } ¡ ¡ interface ¡IEnumerator<out ¡T> ¡: ¡IDisposable ¡ { ¡ ¡ ¡(bool ¡| ¡Exception) ¡MoveNext(); ¡ ¡ ¡T ¡ ¡ ¡ ¡ ¡GetCurrent(); ¡ Discriminated ¡union ¡ } ¡ type ¡(“or”) ¡ Really only two values

  14. What’s ¡the ¡dual ¡of ¡IEnumerable? ¡ interface ¡IEnumerable<out ¡T> ¡ { ¡ ¡ ¡IEnumerator<T> ¡GetEnumerator(); ¡ } ¡ ¡ interface ¡IEnumerator<out ¡T> ¡: ¡IDisposable ¡ { ¡ ¡ ¡(true ¡| ¡false ¡| ¡Exception) ¡MoveNext(); ¡ ¡ ¡T ¡ ¡ ¡ ¡ ¡GetCurrent(); ¡ Got true ? } ¡ Really got T !

  15. What’s ¡the ¡dual ¡of ¡IEnumerable? ¡ interface ¡IEnumerable<out ¡T> ¡ { ¡ ¡ ¡IEnumerator<T> ¡GetEnumerator(); ¡ } ¡ ¡ interface ¡IEnumerator<out ¡T> ¡: ¡IDisposable ¡ { ¡ ¡ ¡(T ¡| ¡false ¡| ¡Exception) ¡MoveNext(); ¡ } ¡ Got false ? Really got void !

  16. What’s ¡the ¡dual ¡of ¡IEnumerable? ¡ interface ¡IEnumerable<out ¡T> ¡ { ¡ ¡ ¡IEnumerator<T> ¡GetEnumerator(); ¡ } ¡ Every enumerator is disposable ¡ interface ¡IEnumerator<out ¡T> ¡: ¡IDisposable ¡ { ¡ ¡ ¡(T ¡| ¡void ¡| ¡Exception) ¡MoveNext(); ¡ } ¡

  17. What’s ¡the ¡dual ¡of ¡IEnumerable? ¡ interface ¡IEnumerable<out ¡T> ¡ { ¡ ¡ ¡(IEnumerator<T> ¡& ¡IDisposable) ¡GetEnumerator(); ¡ } ¡ Hypothe4cal ¡syntax ¡ (“and”) ¡ ¡ interface ¡IEnumerator<out ¡T> ¡ { ¡ ¡ ¡(T ¡| ¡void ¡| ¡Exception) ¡MoveNext(); ¡ } ¡

  18. What’s ¡the ¡dual ¡of ¡IEnumerable? ¡ interface ¡IEnumerable<out ¡T> ¡ { ¡ ¡ ¡(IEnumerator<T> ¡& ¡IDisposable) ¡GetEnumerator(); ¡ } ¡ Variance will flip! ¡ interface ¡IEnumerator<out ¡T> ¡ { ¡ ¡ ¡(T ¡| ¡void ¡| ¡Exception) ¡ Move Next(); ¡ } ¡ Will rename too This is really void

  19. What’s ¡the ¡dual ¡of ¡IEnumerable? ¡ interface ¡IEnumerable<out ¡T> ¡ { ¡ ¡ ¡(IEnumerator<T> ¡& ¡IDisposable) ¡GetEnumerator(); ¡ } ¡ ¡ interface ¡IEnumerator Dual <in ¡T> ¡ { ¡ ¡ ¡void ¡ ¡ ¡ On Next(T ¡| ¡void ¡| ¡Exception); ¡ } ¡ Can encode as three methods

  20. What’s ¡the ¡dual ¡of ¡IEnumerable? ¡ interface ¡IEnumerable<out ¡T> ¡ { ¡ ¡ ¡(IEnumerator<T> ¡& ¡IDisposable) ¡GetEnumerator(); ¡ } ¡ ¡ interface ¡IEnumerator Dual <in ¡T> ¡ { ¡ Color of the ¡ ¡void ¡ ¡ ¡ On Next(T ¡value); ¡ bikeshed (*) ¡ ¡void ¡ ¡ ¡ On Completed(); ¡ ¡ ¡void ¡ ¡ ¡ On Error(Exception ¡exception); ¡ } ¡ (*) ¡Visit ¡h^p://en.wikipedia.org/wiki/Color_of_the_bikeshed ¡

  21. What’s ¡the ¡dual ¡of ¡IEnumerable? ¡ Will leave this interface ¡IEnumerable<out ¡T> ¡ part alone { ¡ ¡ ¡(IEnumerator<T> ¡& ¡IDisposable) ¡GetEnumerator(); ¡ } ¡ This is the data ¡ source Need to patch this one up too interface ¡IObserver<in ¡T> ¡ { ¡ ¡ ¡void ¡ ¡ ¡OnNext(T ¡value); ¡ ¡ ¡void ¡ ¡ ¡OnCompleted(); ¡ ¡ ¡void ¡ ¡ ¡OnError(Exception ¡exception); ¡ } ¡

  22. What’s ¡the ¡dual ¡of ¡IEnumerable? ¡ Color of the interface ¡IEnumerable Dual <out ¡T> ¡ bikeshed (*) { ¡ ¡ ¡IDisposable ¡ Set Observer(IObserver<T> ¡observer); ¡ } ¡ ¡ interface ¡IObserver<in ¡T> ¡ { ¡ ¡ ¡void ¡ ¡ ¡OnNext(T ¡value); ¡ ¡ ¡void ¡ ¡ ¡OnCompleted(); ¡ ¡ ¡void ¡ ¡ ¡OnError(Exception ¡exception); ¡ } ¡ (*) ¡Visit ¡h^p://en.wikipedia.org/wiki/Color_of_the_bikeshed ¡

  23. What’s ¡the ¡dual ¡of ¡IEnumerable? ¡ interface ¡IObservable<out ¡T> ¡ { ¡ ¡ ¡IDisposable ¡Subscribe(IObserver<T> ¡observer); ¡ } ¡ ¡ interface ¡IObserver<in ¡T> ¡ { ¡ ¡ ¡void ¡ ¡ ¡OnNext(T ¡value); ¡ ¡ ¡void ¡ ¡ ¡OnCompleted(); ¡ ¡ ¡void ¡ ¡ ¡OnError(Exception ¡exception); ¡ } ¡

  24. Essen,al ¡Interfaces ¡ interface ¡IObservable<out ¡T> ¡ { ¡ ¡ ¡IDisposable ¡ Subscribe (IObserver<T> ¡observer); ¡ C# 4.0 } ¡ contravariance ¡ interface ¡IObserver<in ¡T> ¡ { ¡ You could get ¡ ¡void ¡ ¡ ¡ On Next(T ¡value); ¡ flooded ¡ ¡void ¡ ¡ ¡ On Error(Exception ¡ex); ¡ ¡ ¡void ¡ ¡ ¡ On Completed(); ¡ } ¡

  25. Essen,al ¡Interfaces ¡ ApplicaHon ¡ ¡ ¡ InteracHve ¡ MoveNext ¡ Got ¡next? ¡ OnNext ¡ ReacHve ¡ Have ¡ next! ¡ IObservable<T> ¡ IEnumerable<T> ¡ IObserver<T> ¡ IEnumerator<T> ¡ Environment

  26. Demo ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend