Chunk - Kalkwst/Risotto GitHub Wiki

Definition

Batches the source sequence into sized chunks and optionally applies a projection on each chunk.

In this page

Overloads

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.

Chunk(int chunkSize)

Batches the source sequence into sized chunks.

public static IEnumerable<IEnumerable<TSource>> Chunk<TSource>(this IEnumerable<TSource> source, int chunkSize);

Type Parameters

TSource
The type of the elements of source.

Parameters

source IEnumerable <TSource>
A sequence that contains the elements to be splitted into batches.

chunkSize
The maximum number of elements in the batch.

Returns

IEnumerable<IEnumerable<TSource>>
A sequence of equally sized chunks containing elements of the source collection.

Exceptions

ArgumentNullException
source is null

-or-

ArgumentOutOfRangeException
if chunkSize is less than or equal to 0

Example

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();

Remarks

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

Chunk(int chunkSize, Func<IEnumerable, TResult>> projector)

Batches the source sequence into sized chunks and applies a projection to each chunk.

Type Parameters

TSource
The type of the elements of source.

TResult
The type of the elements after the projection is applied on each batch

Parameters

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.

Returns

IEnumerable<TResult>
A sequence of projected chunks.

Exceptions

ArgumentNullException
source is null

-or-

ArgumentOutOfRangeException
if chunkSize is less than or equal to 0

-or-

ArgumentNullException
projector is null

Example

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 }

Remarks

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

⚠️ **GitHub.com Fallback** ⚠️