mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-10 06:45:40 +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:
parent
524f3ff092
commit
0123ec60d6
@ -17,40 +17,4 @@ public static partial class EnumerableExtensions
|
|||||||
list.Shuffle(random);
|
list.Shuffle(random);
|
||||||
return list.AsReadOnly();
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user