diff --git a/X10D/src/CollectionExtensions/CollectionExtensions.cs b/X10D/src/CollectionExtensions/CollectionExtensions.cs new file mode 100644 index 0000000..c10f765 --- /dev/null +++ b/X10D/src/CollectionExtensions/CollectionExtensions.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace X10D +{ + /// + /// Extension methods for and . + /// + public static class CollectionExtensions + { + /// + /// Splits into chunks of size . + /// + /// Any type. + /// The collection to split. + /// The maximum length of the nested collection. + /// + /// An containing an of + /// whose lengths are no greater than . + /// + public static IEnumerable> Split(this IReadOnlyCollection value, int chunkSize) + { + if (value is null) + { + throw new ArgumentNullException(nameof(value)); + } + + var count = value.Count; + chunkSize = chunkSize.Clamp(1, count); + + for (var i = 0; i < count / chunkSize; i++) + { + yield return value.Skip(i * chunkSize).Take(chunkSize).ToArray(); + } + } + } +}