using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace mdfinder { public static class Extensions { /// Enumerates into bins of size . /// Generic type parameter. /// The items to act on. /// Size of the bin. /// /// An enumerator that allows foreach to be used to process bin in this collection. /// /// Thanks to @juharr at Stack Overflow. https://stackoverflow.com/a/32970228/1210377 public static IEnumerable> Bin(this IEnumerable items, int binSize) { if(binSize <= 0) { throw new ArgumentOutOfRangeException("binSize", Localization.Localization.BinSizeOutOfRangeExceptionMessage); } return items.Select((x, i) => new { x, i }) .GroupBy(a => a.i / binSize) .Select(grp => grp.Select(a => a.x)); } /// An IEnumerable extension method that returns the first item or a given default value if no items are in the collection. /// Generic type parameter. /// The items to act on. /// The default. /// A T. public static T FirstOr(this IEnumerable items, T @default) { foreach(var t in items) { return t; } return @default; } } }