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);
}
}
}
}