bartde@microsoft.com Slides license: Creative Commons Attribution Non-Commercial Share Alike See http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode
Too hard today (f g)(x) = f(g(x)) Rx is a library for composing asynchronous and event-based programs using observable collections. Queries? LINQ? MSDN DevLabs.NET 3.5 SP1,.NET 4.0 Silverlight 3, Silverlight 4 JavaScript (RxJS) XNA 3.1 for XBOX and Zune Windows Phone 7
interface IEnumerable out IEnumerator Get C# 4.0 covariance interface IEnumerator out IDisposable bool void Move get 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 A B A B A B A B
Because the Dutch are (said to be) cheap A B A B A B A B
The recipe to dualization http://en.wikipedia.org/wiki/dual_(category_theory) Making a U-turn in synchrony Reversing arrows Input becomes output and vice versa
interface IEnumerable IEnumerator interface IEnumerator IDisposable bool void get
interface IEnumerable IEnumerator void interface IEnumerator IDisposable bool void void throws Exception get void C# didn t borrow Java checked exceptions
interface IEnumerable<T> IEnumerator<T> GetEnumerator(void); interface IEnumerator IDisposable bool void throws Exception void
interface IEnumerable<T> (IDisposable & IEnumerator<T>) GetEnumerator(void); interface IEnumerator We really got an enumerator and a disposable bool void throws Exception void
interface IEnumerable<T> IDisposable IEnumerator<T> ) GetEnumerator( void interface IEnumerator Will only dualize the synchrony aspect bool void throws Exception void
interface IEnumerableDual<T> Set IDisposable void IEnumerator interface IEnumerator bool void throws Exception void
interface IEnumerableDual IDisposable IEnumerator<T> x interface IEnumerator This is an output too bool 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 void If you got true, you really got a T
interface IEnumerableDual IDisposable IEnumerator<T> x interface IEnumerator void false Exception void If you got false, you really got void
interface IEnumerableDual IDisposable IEnumerator<T> x); interface IEnumerator<T> (T void Exception) MoveNext( void
interface IEnumerableDual IDisposable IEnumeratorDual<T>); interface IEnumeratorDual<T> Got void (T void Exception)); 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 void void void Exception Source: http://amazon.com
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 interface IObserver in C# 4.0 contravariance void On void On Exception void On You could get flooded
Interactive MoveNext Application Got next? IEnumerable<T> IEnumerator<T> OnNext IObservable<T> IObserver<T> Have next! Reactive Environment
OnCompleted.Empty<int>() new int[0] OnNext.Return(42) new[] { 42 } OnError.Throw<int>(ex) Throwing iterator.never<int>() Notion of time Iterator that got stuck
OnNext OnError OnCompleted.Range(0, 3) OnNext(0) OnNext(1) OnNext(2).Range(0, 3) yield 0 yield 1 yield 2
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 in Console
IObservable int Observable int // Assume we introduce concurrency (see later) IDisposable C# 4.0 named parameter syntax Console "Next: " Console "Oops: " Console "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 "Next: " Console "Oops: " Console "Done" Thread.Sleep(30000); // Main thread is blocked F10
IObservable int Observable int // Assume we introduce concurrency (see later) IDisposable Console "Next: "
IObservable int Observable int // Assume we introduce concurrency (see later) IDisposable Console "Next: " Console "Oops: " Console "Done" Thread.Sleep(30000); // Main thread is blocked F5
IObservable int Observable int // Assume we introduce concurrency (see later) Breakpoint got hit IDisposable Console "Next: " Console "Oops: " Console "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 "MouseMove" var.where( ) Can define operators var.dispose() Resource maintenance!
Exceptions? FileStream File "data.txt" byte new byte new AsyncCallback int Cancel? // Do something with bs[0..bytesread-1] null State? Lack of composition Synchronous completion?
FileStream File "data.txt" Func 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 "TextChanged" var from in select TextBox TimeSpan // Bridge with the dictionary web service var new DictServiceSoapClient var Observable string DictionaryWord // Compose both sources using SelectMany var from in from in select input.selectmany(term => lookup(term))
Re Rea Reac React Reactive Reacti Reactiv R Reactive Reaction Reactive Reactor input Service call 1 Service call 2 UI data binding
Re Rea Reac React Reactive Reacti Reactiv R Reactive Reaction Reactive Reactor input Service call 1 Service call 2 UI data binding Source: http://scrapetv.com
Re Rea Reac React Reactive Reacti Reactiv R Reactive Reaction Reactive Reactor input Service call 1 Service call 2 UI data binding Source: http://scrapetv.com
Re Rea Reac React Reactive Reacti Reactiv R Reactive Reaction Reactive Reactor input Service call 1 Take Until Service call 2 UI data binding Source: http://scrapetv.com
Re Rea Reac React Reactive Reacti Reactiv R Reactive input Service call 1 Service call 2 UI data binding
// IObservable<string> from TextChanged events var Observable EventArgs "TextChanged" var from in select TextBox TimeSpan // Bridge with the dictionary web service var new DictServiceSoapClient var Observable string DictionaryWord // Compose both sources using SelectMany Very local fix var from in from in.takeuntil(input) select
// IObservable<string> from TextChanged events var Observable EventArgs "TextChanged" var from in select TextBox TimeSpan // Bridge with the dictionary web service var new DictServiceSoapClient var Observable string DictionaryWord // Alternative Compose both approach sources for using composition SelectMany using: // var IObservable<T> from Switch<T>(IObservable<IObservable<T>> in sources) var res = (from term in input.takeuntil(input) select select lookup(term)) Hops from source to source.switch();
Fixed (MSIL) Homo-iconic AsQueryable AsQbservable Translatable (Expression trees) How? IQueryable<T> E.g. LINQ to SQL ToQbservable IQbservable<T> E.g. LINQ to PowerShell ToQueryable LINQ to *.* AsObservable AsEnumerable ToObservable IEnumerable<T> E.g. LINQ to Objects IObservable<T> E.g. LINQ to Events ToEnumerable Pull (interactive) Duality Push (reactive) What? Threads Worker pools Message loops Distributed
Way simpler with Rx (f g)(x) = f(g(x)) Rx is a library for composing asynchronous and event-based programs using observable collections. Queries! LINQ! MSDN DevLabs.NET 3.5 SP1,.NET 4.0 Silverlight 3, Silverlight 4 JavaScript (RxJS) XNA 3.1 for XBOX and Zune Windows Phone 7
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