Slides license: Creative Commons Attribution Non-Commercial Share Alike See http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode Too hard today… (𝑓 ◦ 𝑔)(𝑥) = 𝑓(𝑔(𝑥)) Rx is a library for composing asynchronous and event-based programs using observable collections. Queries? LINQ? MSDN DevLabs • .NET 2.0, .NET 4.0, Silverlight • JavaScript, more…? interface IEnumerable out IEnumerator Get C# 4.0 covariance interface IEnumerator out bool Move get void IDisposable You could get stuck (Waiting to move next) C# 4.0 covariance moving on You could get stuck Because the Dutch are (said to be) cheap ¬ 𝐴 ∧ 𝐵 ≡ ¬𝐴 ∨ ¬𝐵 ¬ 𝐴 ∨ 𝐵 ≡ ¬𝐴 ∧ ¬𝐵 The recipe to dualization http://en.wikipedia.org/wiki/Dual_(category_theory) Reversing arrows… Input becomes output and vice versa Making a U-turn in synchrony interface IEnumerable IEnumerator void interface IEnumerator bool void IDisposable void throws Exception get void C# didn’t borrow Java checked exceptions interface IEnumerable<T> GetEnumerator( (IEnumerator<T> & ) void); We really got an enumerator and a disposable interface IEnumerator bool IDisposable void throws Exception void interface IEnumerableDual<T> IEnumerable<T> IDisposable Set IEnumerator<T>) GetEnumerator(void interface IEnumerator bool Will only dualize the synchrony aspect void throws Exception void interface IEnumerableDual IDisposable IEnumerator<T> x interface IEnumerator bool This is an output too void throws Exception void interface IEnumerableDual IDisposable IEnumerator<T> x interface IEnumerator bool Exception void void Discrete domain with true and false interface IEnumerableDual IDisposable IEnumerator<T> x interface IEnumerator T true false Exception void If you got true, you really got a T void interface IEnumerableDual IDisposable IEnumerator<T> x interface IEnumerator void false Exception If you got false, you really got void void interface IEnumerableDual IDisposable IEnumeratorDual>); IEnumerator<T> x); IEnumerator<T> interface IEnumeratorDual<T> Got (T | void | Exception) MoveNext(void But C# doesn’t have discriminated unions… Let’s splat this into three methods instead! interface IEnumerableDual IDisposable IEnumeratorDual<T> interface IEnumeratorDual void void void Exception void interface IObservable IDisposable IObserver<T> observer interface IObserver Exception Source: http://amazon.com void void void interface IObservable out IDisposable Do you really know C# 4.0? IObserver<T> observer Used to detach the observer… interface IObserver in void void void Exception interface IObservable out IDisposable IObserver<T> observer interface IObserver in void void void Exception (*) Visit http://en.wikipedia.org/wiki/Color_of_the_bikeshed interface IObservable out IDisposable Subscribe IObserver C# 4.0 contravariance interface IObserver in void void void On On On Exception You could get flooded MoveNext IEnumerable<T> IEnumerator<T> Environment Have next! IObservable<T> IObserver<T> Reactive Got next? OnNext Interactive Application OnCompleted .Empty<int>() new int[0] OnNext new[] { 42 } .Return(42) OnError .Throw<int>(ex) Throwing iterator .Never<int>() Notion of time Iterator that got stuck OnNext OnError OnNext(0) OnCompleted OnNext(1) OnNext(2) yield 1 yield 2 .Range(0, 3) yield 0 .Range(0, 3) A variant with time notion exists (GenerateWithTime) Observable Hypothetical anonymous iterator syntax in C# new IEnumerable int for int yield return Asynchronous Console Synchronous foreach var Console in IObservable int Observable int // Assume we introduce concurrency (see later)… IDisposable Console Console Console C# 4.0 named parameter syntax "Next: " "Oops: " "Done" C# doesn’t have anonymous interface implementation, so we provide various extension methods that take lambdas. IObservable int Observable int // Assume we introduce concurrency (see later)… IDisposable Console Console Console "Next: " "Oops: " "Done" Thread.Sleep(30000); // Main thread is blocked… F10 IObservable int Observable int // Assume we introduce concurrency (see later)… IDisposable Console Console Console "Next: " "Oops: " "Done" Thread.Sleep(30000); // Main thread is blocked… F10 IObservable int Observable int // Assume we introduce concurrency (see later)… IDisposable Console Console Console "Next: " "Oops: " "Done" Thread.Sleep(30000); // Main thread is blocked… F5 IObservable int Observable int // Assume we introduce concurrency (see later)… Breakpoint got hit IDisposable Console Console Console "Next: " "Oops: " "Done" Thread.Sleep(30000); // Main thread is blocked… .MouseMove args if ( == ) // I’d like to raise another event Lack of composition -= /* what goes here? */ Resource maintenance? IObservable<Point> Observable.FromEvent var .Where( "MouseMove" Can define operators var .Dispose() Resource maintenance! ) Exceptions? FileStream byte File new byte "data.txt" new AsyncCallback int Cancel? // Do something with bs[0..bytesRead-1] Lack of composition null State? Synchronous completion? FileStream Func File "data.txt" IObservable< > Observable.FromAsyncPattern byte new byte // Do something with bs[0..bytesRead-1] Cold .Return .Subscribe( .Subscribe( Hot ) ) .FromEvent " " IScheduler .Return Scheduler.ThreadPool duality .ToObservable(); .ToEnumerable() reactive .Amb( ) .Return Scheduler.ThreadPool " " .ObserveOn(frm) " " push based notion of time // Producing an IObservable<Point> using Select select .Location // Filtering for the first bisector using Where where Asynchronous request React TextChanged Reaction Reactive Reactor Data binding on UI thread // IObservable<string> from TextChanged events var Observable EventArgs var from in select TextBox "TextChanged" TimeSpan // Bridge with the dictionary web service var new DictServiceSoapClient var Observable string DictionaryWord // Compose both sources using SelectMany var from in input.SelectMany(term => from in lookup(term)) select Reactive| Re| React| Reac| Rea| |Reacti| Reactiv| R| Reactive Reaction Reactive Reactor input Service call 1 Service call 2 UI data binding Source: http://scrapetv.com Reactive| Re| React| Reac| Rea| |Reacti| Reactiv| R| input Until Reactive Service call 1 Take Service call 2 UI data binding // IObservable<string> from TextChanged events var Observable EventArgs var from in select TextBox "TextChanged" TimeSpan // Bridge with the dictionary web service var new DictServiceSoapClient var Observable string DictionaryWord Very local fix Compose bothapproach sources for using SelectManyusing: // Alternative composition // Switch<T>(IObservable<IObservable<T>> sources) var IObservable<T> from in var res = (from from term in input .TakeUntil(input) select lookup(term)) Hops from source to source select .Switch(); "#input" "keyUp" function return function return function "#results" function "#results" "Error: " How? (Expression trees) IQueryable<T> IQbservable<T> E.g. LINQ to PowerShell E.g. LINQ to SQL ToQueryable AsQueryable AsObservable AsEnumerable Homo-iconic LINQ to *.* AsQbservable Translatable ToQbservable (MSIL) IEnumerable<T> IObservable<T> E.g. LINQ to Objects E.g. LINQ to Events ToEnumerable Fixed ToObservable Pull (interactive) Threads Worker pools Duality Push What? (reactive) Message loops Distributed Way simpler with Rx (𝑓 ◦ 𝑔)(𝑥) = 𝑓(𝑔(𝑥)) Rx is a library for composing asynchronous and event-based programs using observable collections. Queries! LINQ! MSDN DevLabs • .NET 2.0, .NET 4.0, Silverlight • JavaScript, more…? http://blogs.msdn.com/rxteam http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx http://social.msdn.microsoft.com/Forums/en-US/rx http://channel9.msdn.com/Tags/Rx
© Copyright 2025 Paperzz