1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-10 05:15:43 +00:00

Remove IEnumerable<T>.Split(int)

This functionality has been introduced in .NET 6 with the Chunk method: https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.chunk?view=net-6.0
This commit is contained in:
Oliver Booth 2022-04-20 16:26:15 +01:00
parent 524f3ff092
commit 0123ec60d6
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634

View File

@ -17,40 +17,4 @@ public static partial class EnumerableExtensions
list.Shuffle(random);
return list.AsReadOnly();
}
/// <summary>
/// Splits <paramref name="source" /> into chunks of size <paramref name="chunkSize" />.
/// </summary>
/// <typeparam name="T">Any type.</typeparam>
/// <param name="source">The collection to split.</param>
/// <param name="chunkSize">The maximum length of the nested collection.</param>
/// <returns>
/// An <see cref="IEnumerable{T}" /> containing an <see cref="IEnumerable{T}" /> of <typeparamref name="T" />
/// whose lengths are no greater than <paramref name="chunkSize" />.
/// </returns>
public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> source, int chunkSize)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
var buffer = new List<T>(chunkSize);
foreach (var item in source)
{
buffer.Add(item);
if (buffer.Count >= chunkSize)
{
yield return buffer;
buffer.Clear();
}
}
if (buffer.Count > 0)
{
yield return buffer;
}
}
}