DCSIMG
Rx - for beginners (part 5): marble diagrams, select and where - Bnaya Eshet

Bnaya Eshet

Disclaimer

Rx - for beginners (part 5): marble diagrams, select and where

Rx - for beginners (part 5): marble diagrams, select and where

this post is the 5th in a series of posts about the new Reactive Framework (Rx).

the series TOC can found here.

 Rx, IObservable,IObserver

this post will focus on marble diagrams which is used for visualizing IObservable stream.

on this post we will visualize the select and where clause, while in the

upcoming posts we will discuss other operations that can be

used to upon IObservable streams.

 

the code sample for this post is available here.

 

What should we visualize?

the event stream should visualize the following:

  • on next
  • on error
  • on complete

Rx, IObservable,IObserver

Rx, IObservable,IObserver

 

How should we read the diagram?

marble diagram can have one or more horizontal line.

  • each horizontal line present the timeline of single IObservable stream.
  • the ellipse present single occurrence of new value event (on next).
  • the vertical line present the end of the stream sequence (on complete). 
  • the X sign present exception (on error).

 

The hidden assumption used by the Rx framework is that the IObservable stream

should stop either on complete or on error.

 

How can we draw select clause using the marble diagram?
Code Snippet
  1. // Create IObservable that has increasing long value each second
  2. var timeStream = Observable.Interval(TimeSpan.FromSeconds(1));
  3.  
  4. // invert the timeStream stream using Linq statement
  5. var negativeTimeStream = from value in timeStream
  6.                          select -value;
  7.  
  8. negativeTimeStream.Subscribe(value => Console.WriteLine(value));

line 2, creating IObservable stream that increment the projected value each second.

line 5, is using Linq statement to convert the stream into negative value.

line 8, subscribe to the stream and write it to the console.

the result is shown in the following image.

Rx, IObservable,IObserver 

 

the marble diagrams for the above snippet will look like the following diagram:

Rx, IObservable,IObserver

the marble diagrams show the construction of new IObservable stream using the select operation.

the vertical arrow present the transition from the source stream into the destination stream.

 

as the case with IEnumerable the select statement is extension method which

can be used on any IObservable stream, we will get same result for the following code:

Code Snippet
  1. var negativeTimeStream = timeStream.Select(value => -value);

 

Where clause marble diagram?

the where clause of the following snippet:

Code Snippet
  1. // taking only even values
  2. //var evenTimeStream = timeStream.Where(value => value % 2 == 0);
  3. var evenTimeStream = from value in timeStream
  4.                      where value % 2 == 0
  5.                      select value;

will be present:

Rx, IObservable,IObserver

 

Summary

marble diagrams is how stream manipulation can be visualize.

we already surveyed the select and where operations, and we will continue

with other operation in the upcoming posts.

 

Code Samples

the code sample for this post is available here.

 

תגים של Technorati:‏ ,,

 

kick it on DotNetKicks.com


Comments

No Comments