Code:
public static IEnumerable<T> Yield<T>(this T item) { yield return item; }
public static IEnumerable<T> Yield<T>(this T item) { yield return item; }
public static IEnumerable<IEnumerable<T>> TakeBy<T>(this IEnumerable<T> source, int count) { var grps = source.Select((val, i) => new { idx = i / count, val }).GroupBy(e => e.idx); return grps.Select(g => g.Select(e => e.val)); }
public static IEnumerable<IEnumerable<T>> Combine<T>(this IEnumerable<T> list, int k = 2, bool repetition = false) { for (int i = 0; i < list.Count(); i++) { T cur = list.Skip(i).First(); if (k == 1) { yield return cur.Yield(); } else { foreach (var res in list.Skip(i + (repetition ? 0 : 1)).Combine(k - 1, repetition)) { yield return res.Prepend(cur); } } } }
public static class EnumHelper { public static UInt32 NumFlags(this Enum e) { UInt32 v = Convert.ToUInt32(e); v = v - ((v >> 1) & 0x55555555); v = (v & 0x33333333) + ((v >> 2) & 0x33333333); UInt32 count = ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; return count; } }
struct Lazy<T> { T value; bool init; Func<T> factory; public Lazy(Func<T> f) { factory = f; init = false; value = default(T); } public T Value { get { if (init) return value; else { init = true; return value = factory(); } } } }
struct Lazy<TVal, TArg> { TVal value; TArg arg; bool init; Func<TArg, TVal> factory; public Lazy(Func<TArg, TVal> f, TArg arg) { factory = f; init = false; value = default(TVal); this.arg = arg; } public TVal Value { get { if (init) return value; else { init = true; return value = factory(arg); } } } }
/// <summary> /// <paramref name="source"/>中の要素を<paramref name="count"/> /// 個ごとにまとめ直します。 /// 最後の要素数が足りない場合は足りないまま返します。 /// </summary> public static IEnumerable<IEnumerable<TSource>> TakeBy<TSource>(this IEnumerable<TSource> source, int count) { if (source == null) { throw new ArgumentNullException("source"); } if (count < 1) { throw new ArgumentOutOfRangeException("count"); } while (source.Any()) { yield return source.Take(count); source = source.Skip(count); } }