Logic operators

Logic operators deal with Boolean results, giving the programmer the ability to take decisions on sequence values in a reactive way by producing other sequences. They are the respective of LINQAny, All, and similar operators.

Every/Some/Includes

All these operators produce a new sequence containing a single message that will contain a result of a Boolean question.

The Every operator is the reactive version of the LINQ All operator, returning if all the elements of a sequence comply with a specified statement.

The Some operator is the reactive version of the LINQ Any operator, returning if any element in a sequence complies with a specified statement.

The Includes operator is the reactive version of the LINQ Contains operator, returning if any element in the sequence is the one specified.

Boolean operators, similar to mathematical ones, need the source sequence complete before flowing their response messages.

Here's an example:

var s17 = new Subject<double>(); 
var every = s17.All(x => x > 0); 
var some = s17.Any(x => x % 2 == 0); 
var includes = s17.Contains(4d); 
 
every.Subscribe(x => Console.WriteLine("every: {0}", x)); 
some.Subscribe(x => Console.WriteLine("some: {0}", x)); 
includes.Subscribe(x => Console.WriteLine("includes: {0}", x)); 
 
//some value 
var r = new Random(DateTime.Now.GetHashCode()); 
for (int i = 0; i < 10; i++) 
    s17.OnNext(r.NextDouble() * 100d); 
 
//now operators will flow their message 
s17.OnCompleted(); 

SequenceEqual

The SequenceEqual operator, similar to mathematical and Boolean operators, creates a new sequence that will flow a single message containing the result if multiple source sequences have the same message values respecting the original order, eventually ignoring time-based differences. This means that the two sequences are not synchronized in their message flow timings. The only interest is in the message order and values.

The new sequence will flow its result message only when all the source sequences are complete.

SequenceEqual

A marble diagram showing a SequenceEqual operation

Here's an example:

var s18 = new Subject<int>(); 
var s19 = new Subject<int>(); 
var equals = s18.SequenceEqual(s19); 
equals.Subscribe(x => Console.WriteLine("sequenceEqual: {0}", x)); 
s18.OnNext(10); 
s18.OnNext(20); 
s19.OnNext(10); 
s19.OnNext(20); 
s18.OnNext(30); 
s19.OnNext(30); 
 
//completes to flow out the sequenceEqual result message 
s18.OnCompleted(); 
s19.OnCompleted(); 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.217.220