Chunk - Kalkwst/Risotto GitHub Wiki
Batches the source sequence into sized chunks and optionally applies a projection on each chunk.
In this page
- Definition
- Overloads
- Chunk(int chunkSize)
- Chunk(int chunkSize, Func<IEnumerable, TResult>> projector)
Overload | Description |
---|---|
Chunk(int chunkSize) | Batches the source sequence into sized chunks. |
Chunk(int chunkSize, Func<IEnumerable, TResult> projector) | Batches the source sequence into sized chunks and applies a projection to each chunk. |
Batches the source sequence into sized chunks.
public static IEnumerable<IEnumerable<TSource>> Chunk<TSource>(this IEnumerable<TSource> source, int chunkSize);
TSource
The type of the elements of source
.
source
IEnumerable <TSource>
A sequence that contains the elements to be splitted into batches.
chunkSize
The maximum number of elements in the batch.
IEnumerable<IEnumerable<TSource>>
A sequence of equally sized chunks containing elements of the source collection.
ArgumentNullException
source
is null
-or-
ArgumentOutOfRangeException
if chunkSize
is less than or equal to 0
The following code example demonstrates how to use Chunk(int chunkSize) to batch a sequence into sized chunks.
var result = new int[] { 0, 1, 1, 2, 3, 5, 8, 13, 21 }.Chunk(3);
using var reader = result.GetReader();
reader.Read();
//=> { 0, 1, 1 }
reader.Read();
//=> { 2, 3, 5 }
reader.Read();
//=> { 8, 13, 21 }
reader.ReadEnd();
This function uses deferred execution and streams its results.
When more than one chunk is streamed, all chunks except the last is guaranteed to have chunkSize
elements.
The last chunk may be smaller depending on the remaining elements in the source
sequence.
Warning: Each chunk is pre-allocated to chunkSize
elements. If chunkSize
is set to a very large value, it can lead to an OutOfMemoryException
Batches the source sequence into sized chunks and applies a projection to each chunk.
TSource
The type of the elements of source
.
TResult
The type of the elements after the projection is applied on each batch
source
IEnumerable <TSource>
A sequence that contains the elements to be splitted into batches.
chunkSize
int
The maximum number of elements in the batch.
projector
Func <IEnumerable<TSource>, TResult>
The projection to apply to each bucket.
IEnumerable<TResult>
A sequence of projected chunks.
ArgumentNullException
source
is null
-or-
ArgumentOutOfRangeException
if chunkSize
is less than or equal to 0
-or-
ArgumentNullException
projector
is null
The following code example demonstrates how to use Chunk(int chunkSize, Func<IEnumerable, TResult>> projector) to batch a sequence into sized chunks.
var result = new int[] { 0, 1, 1, 2, 3, 5, 8, 13, 21 }.Chunk(3, batch => batch.Sum());
//=> { 2, 10, 42 }
This function uses deferred execution and streams its results.
When more than one chunk is streamed, all chunks except the last is guaranteed to have chunkSize
elements.
The last chunk may be smaller depending on the remaining elements in the source
sequence.
Warning: Each chunk is pre-allocated to chunkSize
elements. If chunkSize
is set to a very large value, it can lead to an OutOfMemoryException