memo:c#の作法通りにfilter
http://d.hatena.ne.jp/cast_everything/20090828/1251470485
こういうことに立ち向かっていたわけですが、
C#にはdelegateがあるじゃない!
ということで関数に関数投げてウマー!というのはものすごく簡単にできます。
静的な型付けだからさすがにJS見たいな簡単さは無く、「関数ポインタの様なもの」の型宣言は必要デス。
using System; using System.Collections.Generic; using System.Text; class Program { //カンスウポインタの様なもの delegate bool FilterPred(int n); static void Main(string[] args) { //リストにデータ入力 List<int> targ = new List<int>(); for (int i = 0; i < 10; i++) targ.Add(i); //関数に関数を投げる FilterPred f = delegate(int n) { return n > 5; }; List<int> ret = fi(targ, f); //宣言を介さずにいきなり書いてもOK List<int> ret2 = fi(targ, delegate(int n) { return n > 5; } ); //中身確認 foreach (int n in ret) Console.WriteLine("Filter:" + n); while (true) { } } static List<int> fi(List<int> targ, FilterPred f) { List<int> ret = new List<int>(); foreach (int a in targ) { if (f(a)) ret.Add(a); } return ret; } }
delegateを宣言すると自動でBeginInvoke/EndInvokeなぞという「スレッド開くよ!」系の
メソッドも作られる親切仕様。最強過ぎるなぁ。
delegateまで来たのでそろそろevent方向とか非同期threadのあーだこーだとか?
楽しくなってきた。