public const int MaxHandles =64; public static IEnumerable<IEnumerable<T>> DivideIntoChunks<T>(IEnumerable<T> iEnumerable ) { return iEnumerable .Select((x, i) => new { Index = i, Value = x }) .GroupBy(x => x.Index / MaxHandles) .Select(x1 => x1.Select(x2 => x2.Value)) ; }
To jeszcze zamieszczę testy:
[TestCase(1, 1, 1)] [TestCase(64, 1, 64)] [TestCase(65, 2, 64)] [TestCase(128, 2, 64)] [TestCase(129, 3, 64)] [TestCase(0, 0, 0)] public void It_should_divide_into_chunks(int listNumber, int numberOfChunks, int firstChunkCount) { var list = Enumerable.Repeat(1, listNumber); var listOfChunks = Generator.DivideIntoChunks(list); Assert.AreEqual(numberOfChuncks, listOfChunks.Count()); Assert.AreEqual(firstChunkCount, listOfChunks.First().Count()); Assert.AreEqual(listNumber, listOfChunks.SelectMany(x => x).Count()); }
Brak komentarzy:
Prześlij komentarz