タプルを == 演算子で比較、Equals() で比較
using System; class Program { static void Main() { var a = Tuple.Create(1, 2); var b = Tuple.Create(1, 2); Console.WriteLine(a == b); // False Console.WriteLine(a.Equals(b)); // True } }
実行結果です。
False True
タプルを Dictionary のキーにしてみる
using System; using System.Collections.Generic; class Program { static void Main() { var a = Tuple.Create(1, 2); var b = Tuple.Create(1, 2); var dict = new Dictionary<Tuple<int, int>, int>(); dict[a] = 12; Console.WriteLine(dict[a]); // 12 dict[b] = 30; Console.WriteLine(dict[a]); // 30 Console.WriteLine(dict[b]); // 30 } }
実行結果です。
12 30 30
タプルを HashSet に格納
using System; using System.Collections.Generic; class Program { static void Main() { var a = Tuple.Create(1, 2); var b = Tuple.Create(1, 2); var hs = new HashSet<Tuple<int, int>>(); hs.Add(a); Console.WriteLine(hs.Contains(b)); // True } }
実行結果です。
True
2 つの Dictionary をひとつにまとめる
両方の Dictionary に同じキーが含まれる場合は、Sum()
で足し合わせます。
using System; using System.Collections.Generic; using System.Linq; class Program { static Dictionary<int, int> Add(Dictionary<int, int> a, Dictionary<int, int> b) { return a.Concat(b).GroupBy(e => e.Key, e => e.Value).ToDictionary(e => e.Key, e => e.Sum()); } static void Main() { var a = new Dictionary<int, int> { { 1, 2 }, { 2, 3 }, }; var b = new Dictionary<int, int> { { 0, 1 }, { 1, 1 }, { 2, 2 }, { 5, 0 }, }; var c = Add(a, b); foreach (var k in c.Keys.OrderBy(e => e)) { Console.WriteLine("key:{0} val:{1}", k, c[k]); } } }
実行結果です。
key:0 val:1 key:1 val:3 key:2 val:5 key:5 val:0
0,1,0,1,0,1... を繰り返すシーケンス
using System; using System.Collections.Generic; using System.Linq; class Program { static IEnumerable<int> ZeroOne() { int n = 0; while (true) { yield return n; n ^= 1; } } static void Main() { Console.WriteLine(string.Join(",", ZeroOne().Take(10))); // => 0,1,0,1,0,1,0,1,0,1 } }
実行結果です。
0,1,0,1,0,1,0,1,0,1
Pairs 関数
Python の
>>> [(i, j) for i in range(3) for j in range(3)] [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
のような各ペアを求める Pairs 関数を作成してみました。
using System; using System.Collections.Generic; using System.Linq; public static class Iter { static IEnumerable<T[]> Product_1<T>(int p, T[] buf, List<List<T>> pools) { if (p == buf.Length) { yield return buf; } else { foreach (var e in pools[p]) { buf[p] = e; foreach (var ret in Product_1(p+1, buf, pools)) { yield return ret; } } } } public static IEnumerable<T[]> Product<T>(IEnumerable<T> xs, int repeat = 1) { var pools = new List<List<T>>(Enumerable.Repeat(xs.ToList(), repeat)); return Product_1(0, new T[pools.Count], pools); } public static IEnumerable<T[]> Product<T>(IEnumerable<T> xs, IEnumerable<T> ys, int repeat = 1) { var ls = new List<List<T>> { xs.ToList(), ys.ToList() }; var pools = new List<List<T>>(Enumerable.Repeat(ls, repeat).SelectMany(e => e)); return Product_1(0, new T[pools.Count], pools); } public static IEnumerable<U> Pairs<T, U>(IEnumerable<T> xs, IEnumerable<T> ys, Func<T, T, U> fn) { foreach (var e in Product(xs, ys)) { var cs = e.ToArray(); yield return fn(cs[0], cs[1]); } } } class Program { static void Main() { var xs = new[] { 0, 1, 2 }; var ys = Iter.Pairs(xs, xs, (a, b) => Tuple.Create(a, b)); Console.WriteLine(string.Join(" ", ys)); // => (0, 0) (0, 1) (0, 2) (1, 0) (1, 1) (1, 2) (2, 0) (2, 1) (2, 2) } }
Product
を経由せずに、foreach で 2 重ループを回すほうが簡単ですが、Product
の応用を考えていて Pairs
を作れることが分かったので作ってみました。
リンク
Array.Clear() で 2 次元配列をクリアする
Array.Clear メソッドは、1 次元配列だけでなく、2 次元配列もクリアすることが出来ます。
using System; using System.Collections.Generic; using System.Linq; class Program { static void Display(int[,] ary) { for (int i = 0; i < ary.GetLength(0); i++) { for (int j = 0; j < ary.GetLength(1); j++) { Console.Write("{0} ", ary[i, j]); } Console.WriteLine(); } Console.WriteLine(); } static void Main() { var ary = new int[,] { { 1, 2, 3 }, { 4, 5, 6 }, }; Display(ary); // 1 2 3 // 4 5 6 Array.Clear(ary, 1, ary.Length - 1); // ary[0, 0] 以外をクリア Display(ary); // 1 0 0 // 0 0 0 Array.Clear(ary, 0, ary.Length); // 全ての要素をクリア Display(ary); // 0 0 0 // 0 0 0 } }
実行結果です。
1 2 3 4 5 6 1 0 0 0 0 0 0 0 0 0 0 0
リンク
Conditional 属性の練習
using System; using System.Diagnostics; class Program { [Conditional("TEST")] static void Foo() { Console.WriteLine("FOO!"); } static void Main() { Console.WriteLine("BEGIN"); Foo(); Console.WriteLine("END"); } }
実行結果です。
$ mcs -define:TEST a.cs && mono a.exe BEGIN FOO! END $ mcs a.cs && mono a.exe BEGIN END
リンク
Type からインスタンスを生成する
using System; class Foo { public void Say() { Console.WriteLine("Foo!"); } } class Bar { public Bar(int n, string s) { Console.WriteLine("n:{0} s:{1}", n, s); } public void Say() { Console.WriteLine("Bar!"); } } class Program { static void Main() { var foo = (Foo)Activator.CreateInstance(typeof(Foo)); foo.Say(); // Foo! Type barType = typeof(Bar); // typeof() は Type 型を返す var args = new object[] { 123, "bar" }; // Bar のコンストラクタ引数に渡す値 var bar = (Bar)Activator.CreateInstance(barType, args); bar.Say(); // Bar! } }
実行結果です。
Foo! n:123 s:bar Bar!