2016-07-01から1ヶ月間の記事一覧

Butlast メソッド

シーケンスの最後の要素を取り除く Butlast メソッドを作ってみました。 using System; using System.Collections.Generic; using System.Linq; public static class Ext { public static IEnumerable<T> Butlast<T>(this IEnumerable<T> xs) { bool first = true; T </t></t></t>…

Iterate メソッド

初期値と「次の値」を返す関数を引数で受け取り、無限シーケンスを返す Iterate メソッドを作ってみました。 using System; using System.Collections.Generic; using System.Linq; class Iter { public static IEnumerable<T> Iterate<T>(T init, Func<T, T> next) { T </t,></t></t>…

MinBy, MaxBy を作る

using System; using System.Collections.Generic; using System.Linq; public static class Ext { public static T MinBy<T, U>(this IEnumerable<T> xs, Func<T, U> key) where U : IComparable<U> { return xs.Aggregate((a, b) => key(a).CompareTo(key(b)) < 0 ? a : b); </u></t,></t></t,>…

比較関数を渡せる GroupBy メソッドを作る

比較関数を渡せる GroupBy メソッドを作ってみました。 using System; using System.Collections.Generic; using System.Linq; class Program { static IEnumerable<T[]> GroupBy<T>(IEnumerable<T> xs, Func<T, T, bool> match) { var ls = new List<T>(); foreach (var x in xs) { if</t></t,></t></t></t[]>…

1 次元配列の回転

1 次元配列を回転するサンプルです。 using System; using System.Collections.Generic; using System.Linq; class Program { static T[] Rotate<T>(T[] xs, int n) { var ret = new T[xs.Length]; if (ret.Length == 0) return ret; int m = n % ret.Length; f</t>…