From 373313f07e4b364bbf5cd9168a4dbdcd1eefba75 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sat, 18 Apr 2020 14:31:29 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Change=20IE.Chunkify=20to=20I?= =?UTF-8?q?E.Split?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Thereby adding support for any IEnumerable, and naming convention is consistent with .NET and removes warnings of a non-existent word --- X10D/src/ByteExtensions.cs | 22 ---------------------- X10D/src/EnumerableExtensions.cs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 22 deletions(-) create mode 100644 X10D/src/EnumerableExtensions.cs diff --git a/X10D/src/ByteExtensions.cs b/X10D/src/ByteExtensions.cs index 02302cf..3412cfc 100644 --- a/X10D/src/ByteExtensions.cs +++ b/X10D/src/ByteExtensions.cs @@ -20,28 +20,6 @@ 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 . /// diff --git a/X10D/src/EnumerableExtensions.cs b/X10D/src/EnumerableExtensions.cs new file mode 100644 index 0000000..d82b0ff --- /dev/null +++ b/X10D/src/EnumerableExtensions.cs @@ -0,0 +1,31 @@ +namespace X10D +{ + using System.Collections.Generic; + using System.Linq; + + /// + /// Extension methods for . + /// + public static class EnumerableExtensions + { + /// + /// Splits into chunks of size . + /// + /// Any type. + /// The collection to split. + /// The maximum length of the nested collection. + /// Returns an of of + /// values. + public static IEnumerable> Split(this IEnumerable value, int chunkSize) + { + T[] enumerable = value.ToArray(); + var count = enumerable.LongCount(); + chunkSize = chunkSize.Clamp(1, enumerable.Length); + + for (var i = 0; i < (int)(count / chunkSize); i++) + { + yield return enumerable.Skip(i * chunkSize).Take(chunkSize); + } + } + } +}