Bind.cs
C++ でいうところの std::bind1st と std::bind2nd 。なんだけど多分というか十中八九 C# 2.0 以前じゃないとありがたみがない。 lambda expression 使えるなら使ったほうがいいというか使え。
あと C# の delegate まわりの generic paramter 判別がアレなのかおれの理解がアレなのかわからないんだけど System.Predicate<> 用と System.Action<> 用を同時に混在させることができなかったので System.Action<> 用は comment out してある。 List<>.ForEach() が使えなくても foreach で回せばいいしね。
以下恒例の sample code 。
/* * Program.cs * sample codes for Utility.Function * * by janus_wel<janus.wel.3@gmail.com> * This source code is in public domain, and has NO WARRANTY. * */ using System; // for Console, Math using System.Collections.Generic; // List<> using System.Diagnostics; // Debug using Utility; // Function namespace Sample { class Program { static bool Divisible(int x, int y) { return (x % y) == 0; } static void Main(string[] args) { var intList = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7 }; /* * Predicate<> * equivalent codes in C# 3.0 or after * * var divisibleList = intList.FindAll(x => Divisible(x, 3)); * */ var divisibleList = intList.FindAll(Function.Bind2nd<int, int>(Divisible, 3)); Debug.Assert(divisibleList.Count == 3); divisibleList.ForEach(x => Console.WriteLine(x)); var doubleList = intList.ConvertAll(x => (double)x); /* * Converter<> * equivalent codes in C# 3.0 or after * * var powedDoubleList = doubleList.ConvertAll(x => Math.Pow(2, x)); * */ var powedDoubleList = doubleList.ConvertAll( Function.Bind1st<double, double, double>(Math.Pow, 2)); powedDoubleList.ForEach(x => Console.WriteLine(x)); } } }