えムナウ Blog

えムナウ の なすがまま

目次

Blog 利用状況

ニュース


follow mnow at http://twitter.com


えムナウのプログラミングのページ

INETAJ

書庫

日記カテゴリ

ギャラリ

Reactive Extensions for .NET のサンプル その2

Reactive Extensions for .NET のサンプル

System.Interactive.dll が必要です。

http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx

using System;

using System.Collections.Generic;

using System.Linq;

 

namespace Enumerable2

{

    class Program

    {

        static int counter;

 

        static int[] datas = { 1, 2, 3, 4, 5 };

        static int[] datas3 = { 11, 12, 13, 14, 15 };

        static int[] datas4 = { 21, 22, 23, 24, 25 };

 

        static double[] doubledatas = { 0.5, 1.5, 2.5 };

 

        static IEnumerable<int> datasMethod1()

        {

            yield return 1;

            yield return 2;

            yield return 3;

            throw new ApplicationException();

        }

 

        static IEnumerable<int> datasMethod2()

        {

            yield return 6;

            yield return 7;

            yield return 8;

            throw new ApplicationException();

        }

 

        static IEnumerable<int> datasMethod3()

        {

            yield return 11;

            yield return 12;

            yield return 13;

            throw new ApplicationException();

        }

 

        static IEnumerable<int> datasMethod4()

        {

            yield return 1;

            yield return 2;

            yield return 3;

            counter++;

        }

 

        class Person : IDisposable

        {

            public int ID; public string Name;

 

            public void Dispose()

            {

                System.Diagnostics.Debug.WriteLine("Disposed.");

            }

        }

 

        static void Main(string[] args)

        {

            var let = datas.Let(x =>

                {

                    List<int> list = new List<int>();

                    foreach (var n in x)

                    {

                        list.Add(n + 1);

                    }

                    return list;

                }

            );

            foreach (var z in let)

            {

                System.Diagnostics.Debug.WriteLine("Let {0}", z);

            }

 

            var longcount = datas.LongCount(x => x < 3);

            System.Diagnostics.Debug.WriteLine("LongCount {0}", longcount);

 

            var maxenumerable = datas.MaxEnumerable();

            foreach (var z in maxenumerable)

            {

                System.Diagnostics.Debug.WriteLine("MaxEnumerable {0}", z);

            }

 

            var memoize = datas.Memoize();      //caches

            foreach (var z in memoize)

            {

                System.Diagnostics.Debug.WriteLine("Memoize {0}", z);

            }

 

            var memoize2 = datas.Memoize(1024);

            foreach (var z in memoize2)

            {

                System.Diagnostics.Debug.WriteLine("Memoize2 {0}", z);

            }

 

            var memoizeall = datas.MemoizeAll();      //caches

            foreach (var z in memoizeall)

            {

                System.Diagnostics.Debug.WriteLine("MemoizeAll {0}", z);

            }

 

            var minenumerable = datas.MinEnumerable();

            foreach (var z in minenumerable)

            {

                System.Diagnostics.Debug.WriteLine("MinEnumerable {0}", z);

            }

 

            // simple catch

            var onerrorresumenext = EnumerableEx.OnErrorResumeNext(datasMethod1());

            foreach (var z in onerrorresumenext)

            {

                System.Diagnostics.Debug.WriteLine("OnErrorResumeNext {0}", z);

            }

 

            var onerrorresumenext2 = EnumerableEx.OnErrorResumeNext(datasMethod1(),datasMethod2());

            foreach (var z in onerrorresumenext2)

            {

                System.Diagnostics.Debug.WriteLine("OnErrorResumeNext2 {0}", z);

            }

 

            var onerrorresumenext3 = EnumerableEx.OnErrorResumeNext(datasMethod1(), datasMethod2(), datasMethod3());

            foreach (var z in onerrorresumenext3)

            {

                System.Diagnostics.Debug.WriteLine("OnErrorResumeNext3 {0}", z);

            }

 

            // Share & Let

            var prune = datas.Prune(x =>

            {

                List<int> list = new List<int>();

                foreach (var n in x)

                {

                    list.Add(n + 1);

                }

                return list;

            }

            );

            foreach (var z in prune)

            {

                System.Diagnostics.Debug.WriteLine("Prune {0}", z);

            }

 

            // StartWith & Let & Memoizeall

            var publish = datas.Publish(x =>

            {

                List<int> list = new List<int>();

                foreach (var n in x)

                {

                    list.Add(n + 1);

                }

                return list;

            },

            0);

            foreach (var z in publish)

            {

                System.Diagnostics.Debug.WriteLine("Publish {0}", z);

            }

 

            // Serializable & MarshalByRefObject

            var remotable = datas.Remotable();

            foreach (var z in remotable)

            {

                System.Diagnostics.Debug.WriteLine("Remotable {0}", z);

            }

 

            var repeat = EnumerableEx.Repeat(1).Take(5);

            foreach (var z in repeat)

            {

                System.Diagnostics.Debug.WriteLine("Repeat {0}", z);

            }

 

            var repeat2 = EnumerableEx.Repeat(1, 5);

            foreach (var z in repeat2)

            {

                System.Diagnostics.Debug.WriteLine("Repeat2 {0}", z);

            }

 

            var repeat3 = datas.Repeat().Take(6);

            foreach (var z in repeat3)

            {

                System.Diagnostics.Debug.WriteLine("Repeat3 {0}", z);

            }

 

            var repeat4 = datas.Repeat(2);

            foreach (var z in repeat4)

            {

                System.Diagnostics.Debug.WriteLine("Repeat4 {0}", z);

            }

 

            // Memoize & Let

            var replay = datas.Replay(x =>

            {

                List<int> list = new List<int>();

                foreach (var n in x)

                {

                    list.Add(n + 1);

                }

                return list;

            },

            1024);

            foreach (var z in replay)

            {

                System.Diagnostics.Debug.WriteLine("Replay {0}", z);

            }

 

            // Exception Retry

            var retry = datasMethod1().Retry().Take(5);

            foreach (var z in retry)

            {

                System.Diagnostics.Debug.WriteLine("Retry {0}", z);

            }

 

            var retry2 = datasMethod1().Retry(2);

            foreach (var z in retry2)

            {

                System.Diagnostics.Debug.WriteLine("Retry2 {0}", z);

            }

 

            // IEnumerable

            var returnquery = EnumerableEx.Return(3);

            foreach (var z in returnquery)

            {

                System.Diagnostics.Debug.WriteLine("Return {0}", z);

            }

 

            try

            {

                datasMethod1().Run(

                    x => System.Diagnostics.Debug.WriteLine("Run {0}", x),

                    x => System.Diagnostics.Debug.WriteLine("Exception " + x.Message),

                    () => System.Diagnostics.Debug.WriteLine("no error finary")

                );

            }

            catch(Exception)

            {

            }

            finally

            {

                System.Diagnostics.Debug.WriteLine("finary");

            }

 

            EnumerableEx.Repeat(1, 5).Run(x => System.Diagnostics.Debug.WriteLine("Run2 {0}", x));

 

            try

            {

                var doquery = datasMethod1().Do(

                    x => System.Diagnostics.Debug.WriteLine("Do {0}", x),

                    x => System.Diagnostics.Debug.WriteLine("Exception " + x.Message),

                    () => System.Diagnostics.Debug.WriteLine("no error finary")

                );

                doquery.Run();

            }

            catch (Exception)

            {

            }

            finally

            {

                System.Diagnostics.Debug.WriteLine("finary");

            }

 

            var scan = datas.Scan(10, (x, y) => x + y);

            foreach (var z in scan)

            {

                System.Diagnostics.Debug.WriteLine("Scan {0}", z);

            }

 

            var scan0 = datas.Scan0(10, (x, y) => x + y);

            foreach (var z in scan0)

            {

                System.Diagnostics.Debug.WriteLine("Scan0 {0}", z);

            }

 

            var selectmany = datas.SelectMany(doubledatas);

            foreach (var z in selectmany)

            {

                System.Diagnostics.Debug.WriteLine("SelectMany {0}", z);

            }

 

            // lock

            var share = datas.Share();

            foreach (var z in share)

            {

                System.Diagnostics.Debug.WriteLine("Share {0}", z);

            }

 

            var skiplast = datas.SkipLast(3);

            foreach (var z in skiplast)

            {

                System.Diagnostics.Debug.WriteLine("SkipLast {0}", z);

            }

 

            var startwith = datas.StartWith(0);

            foreach (var z in startwith)

            {

                System.Diagnostics.Debug.WriteLine("StartWith {0}", z);

            }

 

            var startwith2 = datas.StartWith(5, 6, 7, 8);

            foreach (var z in startwith2)

            {

                System.Diagnostics.Debug.WriteLine("StartWith2 {0}", z);

            }

 

            var sumenumerable = datas.SumEnumerable();

            foreach (var z in sumenumerable)

            {

                System.Diagnostics.Debug.WriteLine("SumEnumerable {0}", z);

            }

 

            var takelast = datas.TakeLast(3);

            foreach (var z in takelast)

            {

                System.Diagnostics.Debug.WriteLine("TakeLast {0}", z);

            }

 

            var throwquery = EnumerableEx.Throw<int>(new Exception());

            try

            {

                throwquery.Run();

            }

            catch (Exception ex)

            {

                System.Diagnostics.Debug.WriteLine("Throw " + ex.Message);

            }

 

            var usingquery = EnumerableEx.Using(

                () => new Person() { ID = 15, Name = "えムナウ" },

                p => new int[] { p.ID }

            );

            foreach (var z in usingquery)

            {

                System.Diagnostics.Debug.WriteLine("Using {0}", z);

            }

 

            counter = 0;

            var whilequery = EnumerableEx.While(() => counter < 2, datasMethod4());

            foreach (var z in whilequery)

            {

                System.Diagnostics.Debug.WriteLine("While {0}", z);

            }

        }

    }

}

 

 

Let 2
Let 3
Let 4
Let 5
Let 6
LongCount 2
MaxEnumerable 5
Memoize 1
Memoize 2
Memoize 3
Memoize 4
Memoize 5
Memoize2 1
Memoize2 2
Memoize2 3
Memoize2 4
Memoize2 5
MemoizeAll 1
MemoizeAll 2
MemoizeAll 3
MemoizeAll 4
MemoizeAll 5
MinEnumerable 1
OnErrorResumeNext 1
OnErrorResumeNext 2
OnErrorResumeNext 3
'System.ApplicationException' の初回例外が Enumerable2.exe で発生しました。
OnErrorResumeNext2 1
OnErrorResumeNext2 2
OnErrorResumeNext2 3
'System.ApplicationException' の初回例外が Enumerable2.exe で発生しました。
OnErrorResumeNext2 6
OnErrorResumeNext2 7
OnErrorResumeNext2 8
'System.ApplicationException' の初回例外が Enumerable2.exe で発生しました。
OnErrorResumeNext3 1
OnErrorResumeNext3 2
OnErrorResumeNext3 3
'System.ApplicationException' の初回例外が Enumerable2.exe で発生しました。
OnErrorResumeNext3 6
OnErrorResumeNext3 7
OnErrorResumeNext3 8
'System.ApplicationException' の初回例外が Enumerable2.exe で発生しました。
OnErrorResumeNext3 11
OnErrorResumeNext3 12
OnErrorResumeNext3 13
'System.ApplicationException' の初回例外が Enumerable2.exe で発生しました。
Prune 2
Prune 3
Prune 4
Prune 5
Prune 6
Publish 1
Publish 2
Publish 3
Publish 4
Publish 5
Publish 6
Remotable 1
Remotable 2
Remotable 3
Remotable 4
Remotable 5
Repeat 1
Repeat 1
Repeat 1
Repeat 1
Repeat 1
Repeat2 1
Repeat2 1
Repeat2 1
Repeat2 1
Repeat2 1
Repeat3 1
Repeat3 2
Repeat3 3
Repeat3 4
Repeat3 5
Repeat3 1
Repeat4 1
Repeat4 2
Repeat4 3
Repeat4 4
Repeat4 5
Repeat4 1
Repeat4 2
Repeat4 3
Repeat4 4
Repeat4 5
Replay 2
Replay 3
Replay 4
Replay 5
Replay 6
Retry 1
Retry 2
Retry 3
'System.ApplicationException' の初回例外が Enumerable2.exe で発生しました。
Retry 1
Retry 2
Retry2 1
Retry2 2
Retry2 3
'System.ApplicationException' の初回例外が Enumerable2.exe で発生しました。
Retry2 1
Retry2 2
Retry2 3
'System.ApplicationException' の初回例外が Enumerable2.exe で発生しました。
Return 3
Run 1
Run 2
Run 3
'System.ApplicationException' の初回例外が Enumerable2.exe で発生しました。
Exception アプリケーションでエラーが発生しました。
'System.ApplicationException' の初回例外が System.Interactive.dll で発生しました。
finary
Run2 1
Run2 1
Run2 1
Run2 1
Run2 1
Do 1
Do 2
Do 3
'System.ApplicationException' の初回例外が Enumerable2.exe で発生しました。
Exception アプリケーションでエラーが発生しました。
'System.ApplicationException' の初回例外が System.Interactive.dll で発生しました。
finary
Scan 10
Scan 11
Scan 13
Scan 16
Scan 20
Scan 25
Scan0 10
Scan0 11
Scan0 13
Scan0 16
Scan0 20
Scan0 25
SelectMany 0.5
SelectMany 1.5
SelectMany 2.5
SelectMany 0.5
SelectMany 1.5
SelectMany 2.5
SelectMany 0.5
SelectMany 1.5
SelectMany 2.5
SelectMany 0.5
SelectMany 1.5
SelectMany 2.5
SelectMany 0.5
SelectMany 1.5
SelectMany 2.5
Share 1
Share 2
Share 3
Share 4
Share 5
SkipLast 1
SkipLast 2
StartWith 0
StartWith 1
StartWith 2
StartWith 3
StartWith 4
StartWith 5
StartWith2 5
StartWith2 6
StartWith2 7
StartWith2 8
StartWith2 1
StartWith2 2
StartWith2 3
StartWith2 4
StartWith2 5
SumEnumerable 15
TakeLast 3
TakeLast 4
TakeLast 5
'System.Exception' の初回例外が System.Interactive.dll で発生しました。
Throw 種類 'System.Exception' の例外がスローされました。
Using 15
Disposed.
While 1
While 2
While 3
While 1
While 2
While 3

投稿日時 : 2010年7月2日 12:02

コメントを追加

#  Reactive Extensions for .NET ??????????????? ????????? ??????????????? 2010/07/07 16:21 Pingback/TrackBack

Reactive Extensions for .NET ??????????????? ????????? ???????????????

タイトル
名前
URL
コメント