Presentation is loading. Please wait.

Presentation is loading. Please wait.

Paul Using Rx; 8 November 2014 Raleigh Code Camp.

Similar presentations


Presentation on theme: "Paul Using Rx; 8 November 2014 Raleigh Code Camp."— Presentation transcript:

1

2

3

4 Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

5 Show Accelerometer in Action (Demo) Explanation of Reactive Extensions Explanation of Accelerometer Deep Dive into Some Code Today’s Outline try{ Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

6 Switch (AudienceInterest as Pivot) Case Questions: Case WhyIsRxBetterThanEvents: Case WhereToUseRx: Case MoreRxExamples: Case WhatLanguagesHaveAnRxImplementation: Case How2HandleNoise: Case MathsOfAccelerometer : Case WhatsNextInDevelopment: Today’s Outline Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

7 Catch(FailedPresentationException e) { PaulSingsAcapella(Song[“Ill Be Watching You”]); } Finally { Resources On Rx; } Return new WannaTryRxMyself(ref Boss); Today’s Outline } Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

8 Show Accelerometer in Action (Demo) Explanation of Reactive Extensions Explanation of Accelerometer Deep Dive into Some Code Today’s Outline try{ Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

9 Show Accelerometer in Action (Demo) Explanation of Reactive Extensions Explanation of Accelerometer Deep Dive into Some Code Today’s Outline try{ Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

10 Show Accelerometer in Action (Demo) Explanation of Reactive Extensions Explanation of Accelerometer Deep Dive into Some Code Today’s Outline try{ Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

11 LINQ to Events This means: You can use all the benefits of LINQ Sophisticated Query Methods Function Composition Add your own Extension Method It makes Events be 1 st Class Objects What is Reactive Extensions? Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

12 Implementation of the Observer Pattern What is Reactive Extensions? Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp Image Source: http://en.wikipedia.org/wiki/Observer_pattern#mediaviewer/File:Observer.svg

13 The Mathematical Dual of IEnumerable /IEnumerator What is the Mathematical Dual? Any maths concept which is the reverse of its dual Subtraction is the Dual of Addition Boolean Or || is the Dual of And && Contravariance is the Dual of Covariance What is Reactive Extensions? Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

14 Whereas IEnumerable/IEnumerator is consumer-pull-based IEnumerable is the box of blocks IEnumerator is the 3-year-old child pulling blocks out of it Rx is producer-push-based _____________is the pitching machine _____________is the batter observing it What is Reactive Extensions? Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp IObservable IObserver

15 I see IEnumberable all over the place, but I almost never see IEnumerator. If they’re paired, why don’t I use IEnumerator more? Hey Wait A Minute Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp You do. This: Var enumerator = enumerable.GetEnumerator(); While(enumerator.MoveNext()) { var item = enumerator.Current; item.DoSomething(); } is the same as this: foreach(var item in enumerable) { item.DoSomething(); } http://stackoverflow.com/questions/7310454/simple-ienumerator-use-with-example

16 What is Reactive Extensions? Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp Dual-View Comparison of Producer Interfaces Producers Pull-Pattern Producer Push-Pattern Producer Interface Name IEnumerable IObservable InitiationIEnumerator GetEnumerator(); IDisposable Subscribe(IObserver)

17 What is Reactive Extensions? Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp Dual-View Comparison of Consumer Interfaces Consumers Pull-Pattern Consumer Push-Pattern Consumer InterfaceIEnumerator IObserver Get ItemMoveNext() == true item = enum.Current; OnNext(T item)

18 What is Reactive Extensions? Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp Dual-View Comparison of Consumer Interfaces Consumers Pull-Pattern Consumer Push-Pattern Consumer Terminate MoveNext() == falsevoid OnCompleted() Handle Error try/catch blockvoid OnError()

19 What is Reactive Extensions? Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp Dual-View Comparison of Consumer Interfaces Consumers Pull-Pattern Consumer Push-Pattern Consumer Unsubscribe Dispose()? ResetReset()? Dispose() the IDisposable

20 Show Accelerometer in Action (Demo) Explanation of Reactive Extensions Explanation of Accelerometer Deep Dive into Some Code Today’s Outline try{ Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

21 Show Accelerometer in Action (Demo) Explanation of Reactive Extensions Explanation of Accelerometer Deep Dive into Some Code Today’s Outline try{ Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

22 Phidgets USB Accelerometer Phidgets’.Net implementation requires you to register for events Phidgets.Spatial spatial = new Spatial(); spatial.SpatialData += new SpatialDataEventHandler(myMethod); private void myMethod (object sender, SpatialDataEventArgs e) { /*... */ } Brief on Accelerometer and Gyroscope Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

23 What “thing” does the Producer produce? Let’s call it the DirectObject class: The Producer pushes a to the Consumers. Brief on Accelerometer and Gyroscope Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

24 Phidgets gives me a SpatialDataEventArgs via its event. I transform that into my own class: class AccelerometerFrame_raw { public Vector3D Acceleration; public Vector3D RotationRate; public long TimeStampTicks; } // The first level of Brief on Accelerometer and Gyroscope Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

25 I transform a Raw frame into a Processed frame: class AccelerometerFrame_processed { public Vector3D TrueAcceleration; public Vector3D Velocity; public Vector3D Position; public Double SecondsSinceLast; public Vector3D Orientation; } // The second level of Brief on Accelerometer and Gyroscope Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

26 Eventually I will have a smoothed frame class: class AccelerometerFrame_smoothed { public Vector3D TrueAcceleration; public Vector3D Velocity; public Vector3D Position; public Double SecondsSinceLast; public Vector3D Orientation; } // The third level of Brief on Accelerometer and Gyroscope Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

27 What is the producer class? SpatialDataStreamer_raw : IObservable class SpatialDataStreamer_raw2 // has-a public IObservable DeviceDataStream public class SpatialDataStreamer_processed // has-a public IObservable DeviceDataStream Brief on Accelerometer and Gyroscope Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

28 What are the consumer classes? The ViewModel classes for each WPF Window This means they must implement either IObserver or IObserver IObservers must implement OnNext, OnCompleted, and OnError. Brief on Accelerometer and Gyroscope Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

29 Show Accelerometer in Action (Demo) Explanation of Reactive Extensions Explanation of Accelerometer Deep Dive into Some Code Today’s Outline try{ Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

30 Show Accelerometer in Action (Demo) Explanation of Reactive Extensions Explanation of Accelerometer Deep Dive into Some Code Today’s Outline try{ Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

31 Show Accelerometer in Action (Demo) Explanation of Reactive Extensions Explanation of Accelerometer Deep Dive into Some Code Today’s Outline try{ Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

32 Switch (AudienceInterest as Pivot) Case Questions: Case WhyIsRxBetterThanEvents Case WhereToUseRx: Case MoreRxExamples: Case WhatLanguagesHaveAnRxImplementation: Case How2HandleNoise: Case MathsOfAccelerometer: Case WhatsNextInDevelopment: Today’s Outline Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

33 Catch(FailedPresentationException e) { PaulSingsAcapella( Song["Ill Be Watching You"]); } Finally { Resources On Rx; } Return new WannaTryRxMyself(ref Boss); Today’s Outline } Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

34 Luminaries Erik Meijer, Inventor of Rx, @headinthebox Bart de Smet, Principal Software Development Engineer at Microsoft Lee Campbell, Introduction to Rx author, @LeeRyanCampbell Jim Wooley, @jimwooley, thinqlinq.com Resources On Rx Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

35 Getting Started 1. Install Rx from Nuget. Search Nuget for "Rx SDK" to find it Resources On Rx Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

36 Getting Started 2. Watch Curing Your Event Processing Blues with Reactive Extensions (Rx) http://channel9.msdn.com/Events/TechEd/ Europe/2012/DEV413 http://channel9.msdn.com/Events/TechEd/ Europe/2012/DEV413 This is a talk given by Bart de Smet Includes Kinect events and Async/Await Resources On Rx Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

37 Getting Started 3. Read Introduction to Rx by Lee Campbell Read it like a novel (may help with your insomnia). Is Online at http://www.introtorx.com/http://www.introtorx.com/ Is also an ebook on Kindle. Note: Don’t use ISubject. See stack overflow Why are Subjects not recommended in.Net Reactive Extensions? Resources On Rx Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

38 After you’ve started channel9.msdn.com/tags/Rx Jim Wooley’s website, www.thinqlinq.com/www.thinqlinq.com/ Jim Wooley’s presentation: Reactive Extensions (Rx) In Action - Code on the Beach 2014 = www.youtube.com/watch?v=iVhYCCrUHw8 Resources On Rx Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

39 After you’ve started rxwiki.wikidot.com/101samples davesexton.com/blog leecampbell.blogspot.com An Event-driven and Reactive Future - Jonathan Worthington = www.youtube.com/watch?v=_VdIQTtRkb8 Resources On Rx Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

40 After you’ve started Awaiting for Rx: A Play in Four Acts - Paul Betts, Github (youtube.com or evolve.xamarin.com) Mostly about Rx on Xamarin Betts talks about a project of his that converts UI events to Rx (not just for Xam.) Betts works at github Resources On Rx Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

41 Once you’re proficient rxx.codeplex.com Extensions for Reactive Extensions Includes ObservableWeb stuff ObservableNetworkChange Catch, Retry, and OnErrorResumeNext Resources On Rx Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

42 If you really like Category Theory The first paper where Erik Meijer announces his discovery that IEnumerable has a Dual: csl.stanford.edu/~christos/pldi2010.fit/meij er.duality.pdf csl.stanford.edu/~christos/pldi2010.fit/meij er.duality.pdf Expert to Expert: Beckman & Meijer - Inside Rx (youtube or channel9) Heavy Math Here Resources On Rx Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

43 Catch(FailedPresentationException e) { PaulSingsAcapella( Song["Ill Be Watching You"]); } Finally { Resources On Rx; } Return new WannaTryRxMyself(ref Boss); Today’s Outline } Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

44 Catch(FailedPresentationException e) { PaulSingsAcapella( Song["Ill Be Watching You"]); } Finally { Resources On Rx; } Return new WannaTryRxMyself(ref Boss); Today’s Outline } Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

45 paul.schrum@gmail.com Twitter: @philologon StackOverflow: philologon Return new WannaTryRxMyself(ref Boss); Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

46 CLR Events are not 1 st class Objects. Rx IObservables are. CLR Events are not 1 st class Objects. Rx IObservables are. This means you can pass IObservables around from one class or method to another. (Not so Events) This means you can pass IObservables around from one class or method to another. (Not so Events) Because it’s LINQ, you can consume multiple producers, and merge them to make a new producer. Because it’s LINQ, you can consume multiple producers, and merge them to make a new producer. Because it’s LINQ, you can use Lambas. (Not so Events) Because it’s LINQ, you can use Lambas. (Not so Events) Why Rx is Better than Events Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

47 It plays nice with Async/Await It plays nice with Task Helps in isolating state I wrote a concurrent-safe application without even realizing it I wrote a concurrent-safe application without even realizing it Why Rx is Better than Events Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

48 www.introtorx.com/Content/v1.0.10621.0/01 _WhyRx.html#WhyRx www.introtorx.com/Content/v1.0.10621.0/01 _WhyRx.html#WhyRx Infrastructure events like from file watcher, system and WMI events Infrastructure events like from file watcher, system and WMI events Sensors or other Devices UI events like mouse move, button click Domain events like property changed, collection updated, "Order Filled", etc. Domain events like property changed, collection updated, "Order Filled", etc. Where to Use Rx Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

49 From Jim Wooley, Code on the Beach 2014: var sensorValueLow = from s in Sensor where s.SensorValue < 3 select s.SensorValue; var activeLowValSens = sensorValueLow.Subscribe(someVM); More Rx Examples Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

50 From Jim Wooley, Code on the Beach 2014: var diceRoll = from detected in rolldetected.Throttle(400ms) from die in ViewModel.dice.ToObservable().Where (die => !die.Frozen).ObserveOnDispatcher().Do(die =>die.DotCount = null) from result in Randomizer.RollAsync().ToObservable() select new {die, rollDots = result}; More Rx Examples Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

51 From Paul Batts, Xamarin Evolve 2014: theWindow.Events().Resize.Merge(theWindow.Events().Move).Throttle(TimeSpan.FromSeconds(5), RxApp.MainThreadScheduler).Subscribe(_ => SavePlacement()); More Rx Examples Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

52 From Bart de Smet, Curing … Blues, 2012: Var skeletonFrames = Observable. FromEventPattern< SkeletonFrameReadyEventArgs> ( h => kinect.SkeletonFrameReady += h, h => kinect.SkeletonFrameReady -= h ); More Rx Examples Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

53 From Bart de Smet, Curing … Blues, 2012 (part2) : From Bart de Smet, Curing … Blues, 2012 (part2) : var left = from joint in joints let pos = joint[JointType.HandLeft].Position select pos; More Rx Examples Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

54 From Bart de Smet, Curing … Blues, 2012 (part2) : From Bart de Smet, Curing … Blues, 2012 (part2) : var rel = (from pos in left where Math.Abs(pos.X) > 0.2 select pos.X < 0 ? "left" : "right“).DistinctUntilChanged(); More Rx Examples Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp

55 .Net JVM (Java and Scala) JavaScript C++ PHP What Languages Have Rx Paul Schrum @philologon Using Rx; 8 November 2014 Raleigh Code Camp Perl Haskell Swift Python Ruby (eventually)


Download ppt "Paul Using Rx; 8 November 2014 Raleigh Code Camp."

Similar presentations


Ads by Google