Esempio n. 1
0
        static void MainA(string[] args)
        {
            var values = Observable.Range(1, 5);

            values.Inspect("values");
            values.Count().Inspect("count");

            var intSubj = new Subject <int>();

            intSubj.Inspect("intSubj");
            intSubj.Min().Inspect("min");     // not a running minimum!
            intSubj.Sum().Inspect("sum");     // requires all values
            intSubj.Average().Inspect("avg"); // double!

            intSubj.OnNext(2);
            intSubj.OnNext(4);
            intSubj.OnNext(1);
            intSubj.OnCompleted();

            // first, last, single
            var replay = new ReplaySubject <int>();

            // later
            replay.OnNext(-1); // doesn't complete unless OnCompleted
            replay.OnNext(2);

            replay.OnCompleted();

            replay.FirstAsync(i => i > 0).Inspect("FirstAsync"); // doesn't require completion

            replay.FirstOrDefaultAsync(i => i > 10)              // yields 0 because no element matches
            .Inspect("FirstOrDefaultAsync");                     // doesn't require completion

            replay.SingleAsync().Inspect("SingleAsync");         // try commenting out one of OnNext
            // requires completion! try commenting out OnCompleted -> no output

            //replay.SingleOrDefaultAsync()

            // Sum is always the final value, how about a running sum?
            var subj  = new Subject <double>();
            int power = 1;

            subj.Aggregate(0.0, (p, c) => p + Math.Pow(c, power++)).Inspect("poly");
            subj.OnNext(1, 2, 4).OnCompleted(); // 1^1 + 2^2 + 4^3

            // running sum? no problem
            var subj2 = new Subject <double>();

            subj2.Scan(0.0, (p, c) => p + c).Inspect("scan"); // same as Aggregate().TakeLast()
            subj2.OnNext(1, 2, 3);                            //.OnCompleted();
                                                              // OnCompleted doesn't really matter anymore
        }
Esempio n. 2
0
        public static void Main(string[] args)
        {
            var values = Observable.Range(1, 5);

            values.Inspect("Values");
            values.Count().Inspect("Count");
            values.Average().Inspect("Average");
            values.Max().Inspect("Max");
            values.Min().Inspect("Min");
            values.Sum().Inspect("Sum");

            var replay = new ReplaySubject <int>();

            replay.OnNext(-1, 2);
            replay.OnCompleted();

            replay.FirstAsync(i => i > 0).Inspect("First Async");

            var replay2 = new ReplaySubject <int>();

            replay2.OnNext(-1);
            replay2.OnCompleted();

            replay2.FirstOrDefaultAsync(i => i > 0).Inspect("First Or Default Async");

            var replay3 = new ReplaySubject <int>();

            replay3.OnNext(-1);
            replay3.OnCompleted();

            replay3.SingleAsync().Inspect("Single Async");

            var replay4 = new ReplaySubject <int>();

            replay4.OnNext(-1, 2);
            replay4.OnCompleted();

            replay4.SingleOrDefaultAsync().Inspect("Single Or Default Async");

            var subj  = new Subject <double>();
            int power = 1;

            subj.Aggregate(0.0, (p, c) => p + Math.Pow(c, power++)).Inspect("Poly");
            subj.OnNext(1, 2, 4);
            subj.OnCompleted();

            var subj2 = new Subject <double>();

            subj2.Scan(0.0, (p, c) => p + c).Inspect("Scan");
            subj2.OnNext(1, 2, 3);
        }