えムナウ Blog

えムナウ の なすがまま

目次

Blog 利用状況

ニュース


follow mnow at http://twitter.com


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

INETAJ

書庫

日記カテゴリ

ギャラリ

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

Reactive Extensions for .NET のサンプル

System.Interactive.dll が必要です。

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

 

Console アプリケーションじゃないのはなぜって質問はスルーの方向で。

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Windows;

 

namespace Enumerable

{

    public partial class MainWindow : Window

    {

        int counter;

 

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

        int[] datas2 = { };

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

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

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

 

        IEnumerable<int> datasMethod1()

        {

            yield return 1;

            yield return 2;

            yield return 3;

            throw new ApplicationException();

        }

 

        IEnumerable<int> datasMethod2()

        {

            yield return 6;

            yield return 7;

            yield return 8;

            throw new ApplicationException();

        }

 

        IEnumerable<int> datasMethod3()

        {

            yield return 11;

            yield return 12;

            yield return 13;

            throw new ApplicationException();

        }

 

        IEnumerable<int> datasMethod4()

        {

            yield return 1;

            yield return 2;

            yield return 3;

            counter++;

        }

 

        class Person { public int ID; public string Name; }

        Person[] persons =

        {

            new Person() { ID=1, Name="中さん" },

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

            new Person() { ID=34, Name="R・田中一郎"}

        };

 

        public MainWindow()

        {

            InitializeComponent();

 

            var aggregateEnumerable = datas.AggregateEnumerable((x, y) => x + y);

            foreach (var z in aggregateEnumerable)

            {

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

            }

 

            var allEnumerable = datas.AllEnumerable(x => x > 0);

            foreach (var z in allEnumerable)

            {

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

            }

 

            var anyEnumerable = datas.AnyEnumerable(x => x > 4);

            foreach (var z in anyEnumerable)

            {

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

            }

 

            var anyEnumerable2 = datas2.AnyEnumerable();

            foreach (var z in anyEnumerable2)

            {

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

            }

 

            var averageEnumerable = datas.AverageEnumerable();

            foreach (var z in averageEnumerable)

            {

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

            }

 

            var bufferWithCount = datas.BufferWithCount(2);

            foreach (var list in bufferWithCount)

            {

                System.Diagnostics.Debug.WriteLine("BufferWithCount list {0}", list);

                foreach (var z in list)

                {

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

                }

            }

 

            var casequery = EnumerableEx.Case(

                ()=>1,

                new Dictionary<int,IEnumerable<int>>(){

                    {1,datas},{2,datas2}        // switch case

                },

                datasMethod1()                        // default

            );

            foreach (var z in casequery)

            {

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

            }

 

            var catchquery = datasMethod1().Catch((Exception e) => System.Linq.Enumerable.Empty<int>());

            foreach (var z in catchquery)

            {

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

            }

 

            try

            {

                var catchquery2 = datasMethod1().Catch((SystemException e) => System.Linq.Enumerable.Empty<int>());

                foreach (var z in catchquery2)

                {

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

                }

            }

            catch (Exception ex)

            {

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

            }

           

            var catchquery3 = datasMethod1().Catch(

                (Exception e) => System.Linq.Enumerable.Empty<int>())       // try catch

                .Finally(() => System.Diagnostics.Debug.WriteLine("finary"));     //finary

            foreach (var z in catchquery3)

            {

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

            }

 

            var catchquery4 = datasMethod1().Catch(datasMethod2());

            foreach (var z in catchquery4)

            {

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

            }

 

            var catchquery5 = EnumerableEx.Catch(datasMethod1(),datasMethod2(),datasMethod3());

            foreach (var z in catchquery5)

            {

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

            }

 

            var concat = datas.Concat(datas3);

            foreach (var z in concat)

            {

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

            }

 

            var concat2 = EnumerableEx.Concat(datas,datas3,datas4);

            foreach (var z in concat2)

            {

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

            }

 

            var contains = datas.ContainsEnumerable(3);

            foreach (var z in contains)

            {

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

            }

 

            var count = datas.CountEnumerable();

            foreach (var z in count)

            {

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

            }

 

            var textvalue = "ONE";

            var defar = EnumerableEx.Defer(() =>

                {

                    if (textvalue == "ONE") return datas;

                    if (textvalue == "TWO") return datas2;

                    return datas3;

                }

            );

            foreach (var z in defar)

            {

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

            }

 

            var distinct = datas5.Distinct();

            foreach (var z in distinct)

            {

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

            }

 

            var distinctUntilChanged = datas5.DistinctUntilChanged();

            foreach (var z in distinctUntilChanged)

            {

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

            }

 

            counter = 0;

            var dowhile = datasMethod4().DoWhile(() => counter < 2);

            foreach (var z in dowhile)

            {

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

            }

 

            var forquery = EnumerableEx.For(persons, p => new int[] { p.ID });

            foreach (var z in forquery)

            {

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

            }

 

            var forkjoin = EnumerableEx.ForkJoin(datas, datas3, datas4);

            foreach (var array in forkjoin)

            {

                System.Diagnostics.Debug.WriteLine("ForkJoin array {0}", array);

                foreach (var z in array)

                {

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

                }

            }

 

            var generate = EnumerableEx.Generate(0, x => x < 5, x => x, x => x + 1);

            foreach (var z in generate)

            {

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

            }

 

            var ifquery = EnumerableEx.If(() => true, datas, datas3);

            foreach (var z in ifquery)

            {

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

            }

 

            bool isempty = datas2.IsEmpty();

            System.Diagnostics.Debug.WriteLine("IsEmpty {0}", isempty);

 

            var isempty2 = datas2.IsEmptyEnumerable();

            foreach (var z in isempty2)

            {

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

            }

        }

    }

}

 

AggregateEnumerable 15
AllEnumerable True
AnyEnumerable True
AnyEnumerable2 False
AverageEnumerable 3
BufferWithCount list System.Collections.Generic.List`1[System.Int32]
BufferWithCount 1
BufferWithCount 2
BufferWithCount list System.Collections.Generic.List`1[System.Int32]
BufferWithCount 3
BufferWithCount 4
BufferWithCount list System.Collections.Generic.List`1[System.Int32]
BufferWithCount 5
EnumerableEx.Case 1
EnumerableEx.Case 2
EnumerableEx.Case 3
EnumerableEx.Case 4
EnumerableEx.Case 5
Catch 1
Catch 2
Catch 3
'System.ApplicationException' の初回例外が Enumerable.exe で発生しました。
Catch2 1
Catch2 2
Catch2 3
'System.ApplicationException' の初回例外が Enumerable.exe で発生しました。
'System.ApplicationException' の初回例外が System.Interactive.dll で発生しました。
Catch2 exception アプリケーションでエラーが発生しました。
Catch3 1
Catch3 2
Catch3 3
'System.ApplicationException' の初回例外が Enumerable.exe で発生しました。
finary
Catch4 1
Catch4 2
Catch4 3
'System.ApplicationException' の初回例外が Enumerable.exe で発生しました。
Catch4 6
Catch4 7
Catch4 8
'System.ApplicationException' の初回例外が Enumerable.exe で発生しました。
Catch5 1
Catch5 2
Catch5 3
'System.ApplicationException' の初回例外が Enumerable.exe で発生しました。
Catch5 6
Catch5 7
Catch5 8
'System.ApplicationException' の初回例外が Enumerable.exe で発生しました。
Catch5 11
Catch5 12
Catch5 13
'System.ApplicationException' の初回例外が Enumerable.exe で発生しました。
Concat 1
Concat 2
Concat 3
Concat 4
Concat 5
Concat 11
Concat 12
Concat 13
Concat 14
Concat 15
Concat2 1
Concat2 2
Concat2 3
Concat2 4
Concat2 5
Concat2 11
Concat2 12
Concat2 13
Concat2 14
Concat2 15
Concat2 21
Concat2 22
Concat2 23
Concat2 24
Concat2 25
Contains True
CountEnumerable 5
Defer 1
Defer 2
Defer 3
Defer 4
Defer 5
Distinct 1
Distinct 2
Distinct 3
Distinct 4
Distinct 5
DistinctUntilChanged 1
DistinctUntilChanged 2
DistinctUntilChanged 3
DistinctUntilChanged 4
DistinctUntilChanged 1
DistinctUntilChanged 4
DistinctUntilChanged 5
DoWhile 1
DoWhile 2
DoWhile 3
DoWhile 1
DoWhile 2
DoWhile 3
For 1
For 15
For 34
ForkJoin array System.Int32[]
ForkJoin 5
ForkJoin 15
ForkJoin 25
Generate 0
Generate 1
Generate 2
Generate 3
Generate 4
If 1
If 2
If 3
If 4
If 5
IsEmpty True
IsEmptyEnumerable True

投稿日時 : 2010年7月1日 18:50

コメントを追加

No comments posted yet.
タイトル
名前
URL
コメント