Sunday, April 15, 2012

Creating Reactive Streams

Here are some samples which I find useful when using Rx for purpose of figuring out various constructs. Typically I use Observable.Interval to create streams of any kind:

Following will create a stream of random ints:

Following will create a stream of random ints between 10 and 1000:

We can also use Select to convert the stream to a stream of booleans. Following creates alternating True, False never ending stream; ticking one per second:

Following creates a random stream of bools:

Stream of random doubles:

Stream of random doubles between two numbers. I am using GetRandomNumber method from here. Following will create a infinite stream of a doubles between 5 and 20 ticking one per second:

If I were to generate random prices for a particular security, I can do it like this. This will generate Bid and Ask continuously with Bid being always greater than a minimum price and ask being greater than bid:

Above query shows the following results in console:


The equivalent query in Linqpad is (Setting up Linqpad for Rx is here):

This shows up like this in LinqPad:





No comments:

Post a Comment