Verisk Analytics, Inc. All rights reserved. Confidential and Proprietary A not-comprehensive introduction. February 8, 2016 1 Reactive Extensions .NET George Taskos
Verisk Analytics, Inc. All rights reserved. Confidential and Proprietary “Functional programming allows developers to describe what they want to do, rather than forcing them to describe how they want to do it.” Anders Hejlsberg, C# Creator 2
Verisk Analytics, Inc. All rights reserved. Confidential and Proprietary Why? • Parallel programming with .NET is tricky • Error Handling • Cancellation • Easy scheduling (timeout, throttling) • Easier to test 5
Verisk Analytics, Inc. All rights reserved. Confidential and Proprietary Sequences ARE COOL • Sequences are a list of things, in a particular order, that may or may not end • In C#, one very common sequence is IEnumerable<T> • Sequences are lazy 6
Verisk Analytics, Inc. All rights reserved. Confidential and Proprietary var thisDoesntTakeAnyTimeAtAll = Enumerable.Range(0, 100000) .Select(x => { Thread.Sleep(1000); return x*5; }); Console.WriteLine(”This happens instantly!”); 7
Verisk Analytics, Inc. All rights reserved. Confidential and Proprietary Events in .NET this.KeyUp += (sender, args) => { /* Act on this event. I want to combine with another one, so difficult to compose events! */ } this.KeyUp -= /* Nope, anonymous handlers cannot be cleaned */ • Resource management? OMG! • Events are just weird, and definitely NOT cool 8
Verisk Analytics, Inc. All rights reserved. Confidential and Proprietary More about events (not cool!) • Events show up everywhere in .NET • Events are not compositional • var thatEvent = myButton.Clicked; 9 >> V >> e textBox.KeyUp += (o, e) => { Console.WriteLine(e.Char); } >> r >> i >> s >> k
Verisk Analytics, Inc. All rights reserved. Confidential and Proprietary • Hmmm…”Sequences are a list of things, in a particular order, that may or may not end.”, is that like events? 10 Better events? // Try this with events IObservable<string> userTypedVerisk = textBox.KeyUp .Buffer(5 /*items*/, 1 /*at a time*/) .Select(x => String.Join(x)) /* Join every 5 chars */ .Where(x => x.Equals(“Verisk”)); /* Check equality */
Verisk Analytics, Inc. All rights reserved. Confidential and Proprietary How do you create observables? • From arrays • From a single value • From .NET Events • From Tasks • Manually, using Subject<T> 14
Verisk Analytics, Inc. All rights reserved. Confidential and Proprietary LINQ is our superhero! // Hot, will return results on subscribe, we set it up and will // evaluate when we asked for the values var openOrdersObservable = context.Orders .Where(o => o.UserId == userId && o.Status == OrderStatus.Open) .Take(100) .ToObservable(); // Subscribe is your foreach, I’ll let you know the results now IDisposable openOrdersSubscription = openOrdersObservable .Subscribe(orders => /* Do stuff */); // Clean up when you need, ah, disposable! openOrdersSubscription.Dispose() 16
Verisk Analytics, Inc. All rights reserved. Confidential and Proprietary Concurrency Many operators in Rx introduce concurrency • Throttle • Interval • Delay • BufferWithTime Generally they provide an overload to supply a Scheduler • ImmediateScheduler – Static Immediate • CurrentThreadScheduler – Placed on a queue for the current thread • NewThreadScheduler – Spawn a new Thread • DispatcherScheduler - Silverlight • TaskPoolScheduler - TPL • ThreadPoolScheduler 17
Verisk Analytics, Inc. All rights reserved. Confidential and Proprietary Synchronization var observable= Observable .Return(“Hello Rx”, Scheduler.ThreadPool); /* Cross-thread operation, not valid */ observable.Subscribe(p => label.Text = p); /* No problem, we got this */ observable.ObserveOn (RxApp.MainThreadScheduler).Subscribe(p => label.Text = p); 18
Verisk Analytics, Inc. All rights reserved. Confidential and Proprietary ReactiveUI • A MVVM framework that integrates with the Reactive Extensions for .NET to create elegant, testable User Interfaces that run on any mobile or desktop platform 19