Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Reactive Extensions .NET

Reactive Extensions .NET

A not-comprehensive introduction

Avatar for Georgios Taskos

Georgios Taskos

February 08, 2016

Other Decks in Programming

Transcript

  1. Verisk Insurance Solutions | ISO AIR Worldwide Xactware © 2015

    Verisk Analytics, Inc. All rights reserved. Confidential and Proprietary A not-comprehensive introduction. February 8, 2016 1 Reactive Extensions .NET George Taskos
  2. Verisk Insurance Solutions | ISO AIR Worldwide Xactware © 2015

    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
  3. Verisk Insurance Solutions | ISO AIR Worldwide Xactware © 2015

    Verisk Analytics, Inc. All rights reserved. Confidential and Proprietary So, what is it? 3
  4. Verisk Insurance Solutions | ISO AIR Worldwide Xactware © 2015

    Verisk Analytics, Inc. All rights reserved. Confidential and Proprietary Everybody loves Rx Languages • Java: RxJava (NETFLIX, used for server-side concurrency) • JavaScript: RxJS (Autocomplete, drag&drop, animation, etc.) • C#(Unity): UniRx • Scala: RxScala • Clojure: RxClojure • C++: RxCpp • Ruby: Rx.rb • Python: RxPY • Groovy: RxGroovy • JRuby: RxJRuby • Kotlin: RxKotlin • Swift: RxSwift ReactiveX for platforms and frameworks • RxNetty • RxAndroid • RxCocoa • RxUI 4
  5. Verisk Insurance Solutions | ISO AIR Worldwide Xactware © 2015

    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
  6. Verisk Insurance Solutions | ISO AIR Worldwide Xactware © 2015

    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
  7. Verisk Insurance Solutions | ISO AIR Worldwide Xactware © 2015

    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
  8. Verisk Insurance Solutions | ISO AIR Worldwide Xactware © 2015

    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
  9. Verisk Insurance Solutions | ISO AIR Worldwide Xactware © 2015

    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
  10. Verisk Insurance Solutions | ISO AIR Worldwide Xactware © 2015

    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 */
  11. Verisk Insurance Solutions | ISO AIR Worldwide Xactware © 2015

    Verisk Analytics, Inc. All rights reserved. Confidential and Proprietary Collections in .NET interface IEnumerable<out T> { IEnumerator<T> GetEnumerator(); } interface IEnumerator<out T> : IDisposable { bool MoveNext(); T Current { get; } void Reset(); } 11
  12. Verisk Insurance Solutions | ISO AIR Worldwide Xactware © 2015

    Verisk Analytics, Inc. All rights reserved. Confidential and Proprietary Reactive observable streams public interface IObservable<out T> { IDisposable Subscribe(IObserver<T> observer); } public interface IObserver<in T> { void OnNext(T value); // Next value void OnError(Exception error); // Oops! Error! void OnCompleted(); // That’s all folks } 12
  13. Verisk Insurance Solutions | ISO AIR Worldwide Xactware © 2015

    Verisk Analytics, Inc. All rights reserved. Confidential and Proprietary Pull enumerable values (give me what you have) • In memory collections • Database queries • Generated sequences • Message queues • … Push to subscriber (don’t call me, I will call you) • Event streams • Asynchronous computations • Asynchronous queries • Asynchronous enumerations • … 13
  14. Verisk Insurance Solutions | ISO AIR Worldwide Xactware © 2015

    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
  15. Verisk Insurance Solutions | ISO AIR Worldwide Xactware © 2015

    Verisk Analytics, Inc. All rights reserved. Confidential and Proprietary Composability • LINQ (Where, Take, Skip, Sum, Max, Any, …) • Statements (If, Case, For, While, DoWhile) • Combinators (Merge, Zip, Amb, …) • Exceptions (Catch, Retry, ...) • Rx (Delay, Throttle, Interval, Publish, ...) 15
  16. Verisk Insurance Solutions | ISO AIR Worldwide Xactware © 2015

    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
  17. Verisk Insurance Solutions | ISO AIR Worldwide Xactware © 2015

    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
  18. Verisk Insurance Solutions | ISO AIR Worldwide Xactware © 2015

    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
  19. Verisk Insurance Solutions | ISO AIR Worldwide Xactware © 2015

    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
  20. Verisk Insurance Solutions | ISO AIR Worldwide Xactware © 2015

    Verisk Analytics, Inc. All rights reserved. Confidential and Proprietary Resources • ReactiveX - http://reactivex.io/ • ReactiveUI - http://reactiveui.net/ • Beginner’s Guide to the Rx - http://bit.ly/1O0mFl0 • The Reactive Manifesto - http://bit.ly/1tw0qgj • Intro to Rx - http://www.introtorx.com/ • Programming Rx & LINQ - http://bit.ly/1K8u3jA • Demo source code - http://bit.ly/1nTQ3nG 20
  21. Verisk Insurance Solutions | ISO AIR Worldwide Xactware © 2015

    Verisk Analytics, Inc. All rights reserved. Confidential and Proprietary © 2015 Verisk Analytics, Inc. All rights reserved. Confidential and Proprietary Demo Let’s be Reactive!