Add a "ChunkAsync" extension method for IAsyncEnumerable#1756
Add a "ChunkAsync" extension method for IAsyncEnumerable#1756JasonWei512 wants to merge 2 commits intodotnet:mainfrom
Conversation
|
Neat idea but atleast one alloc would be wasted (The list won't be reused && the growth of the list) |
e2f29d5 to
f8a1797
Compare
|
IMHO this solution is a bit too complicated. Do we really need to return an Array? If not, we could just use a List or wrap it in a ReadOnlyList. This is what i came up with: public static async IAsyncEnumerable<IReadOnlyCollection<TSource>> ChunkAsync<TSource>
(
this IAsyncEnumerable<TSource> source,
int size,
[EnumeratorCancellation] CancellationToken cancellationToken = default
)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (size < 1)
throw new ArgumentOutOfRangeException(nameof(size));
await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
var buffer = new List<TSource>(size);
while (await e.MoveNextAsync())
{
buffer.Add(e.Current);
if (buffer.Count >= size)
{
yield return buffer;
buffer = new List<TSource>(size);
}
}
if (buffer.Count > 0)
{
yield return buffer;
}
}Now the list is initialized with a capacity of the length - no idea if thats good or not.... |
|
Is this repository still maintained? |
|
@JKamsker apparently some people like to turn off chunking conditionally by passing a large |
|
@theodorzoulias Lol yea then it might be the better option :D |
An async version of .NET 6's
IEnumerable.Chunk(int size)(Link).It splits an
IAsyncEnumerable<T>intoT[]s ofsize.