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)); } } }