From 05edb47f928035ea1eef8cd3304b50cbc6a1d644 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Tue, 24 Dec 2019 16:16:40 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20IEnumerable.Chunkify(in?= =?UTF-8?q?t)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- X10D/src/ByteExtensions.cs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/X10D/src/ByteExtensions.cs b/X10D/src/ByteExtensions.cs index 7f03889..0469cb4 100644 --- a/X10D/src/ByteExtensions.cs +++ b/X10D/src/ByteExtensions.cs @@ -24,6 +24,28 @@ return BitConverter.ToString(bytes.ToArray()); } + /// + /// Splits into chunks of size . + /// + /// The collection to split. + /// The maximum length of the nested collection. + /// Returns an of of + /// values. + public static IEnumerable> Chunkify(this IEnumerable bytes, int chunkSize) + { + IEnumerable enumerable = bytes as byte[] ?? bytes.ToArray(); + long count = enumerable.LongCount(); + List> chunks = new List>(); + chunkSize = chunkSize.Clamp(1, enumerable.Count()); + + for (int i = 0; i < (int) (count / chunkSize); i++) + { + chunks.Add(enumerable.Skip(i * chunkSize).Take(chunkSize)); + } + + return chunks.ToArray(); + } + /// /// Converts the [] to an . ///