Runtime: IEnumerable should have a extension for creating fixed size chunks

Created on 22 Sep 2018  ·  13Comments  ·  Source: dotnet/runtime

Background and Motivation

There's currently no method available to create chunks from an IEnumerable<T>. It's a popular functionality as this comment shows.
I believe most projects in need of this functionality have their own extension to IEnumerable<T> and the goal of this proposal is to have System.Linq offer this functionality

Proposed API

namespace System.Linq
{
    public static class Enumerable
    {
+        public static IEnumerable<T[]> ChunkBy(this IEnumerable<T> source, int maxSize);
    }
}

Usage Examples

Another way to create chunks would be to specify the amount of chunks you would like to have from an IEnumerable<T> but I think it would be more desirable to have it as a separate method.
like the sample below.

// Where size determines the split amount.
public static IEnumerable<T[]> SplitBy(this IEnumerable<T> source, int size)
{
    ...
}

int[] numbers = new int[]{1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9, 10}
IEnumerable<int[]> numberChunks = numbers.SplitBy(2) // {[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]}

Risks

This could potentially be a breaking change for users who have their own extension methods due to naming collisions.

api-ready-for-review area-System.Linq

Most helpful comment

Chunking is a useful feature which we should consider adding to System.Linq. My preferred signature would be the following:

public static class Enumerable
{
     public static IEnumerable<T[]> ChunkBy(this IEnumerable<T> source, int size);
}

F# core already has a corresponding chunkBySize function.

All 13 comments

GroupBy?

@inputfalken If you want to add a new API to .Net Core, you should follow the API review process. That means not opening a PR before the API is accepted and also including a speclet in this issue.

@benaadams Maybe this is redundant.

But I think it would be nice to have the option to easily create fixed size sub-sequences by just providing an integer.

Also note that there already was a proposal for this in the past, which was closed: https://github.com/dotnet/corefx/issues/2146.

@svick Oh i didn't see that one 😐, should I remove the PR then?

@inputfalken Yes, I think the PR should be closed for now.

Is there anything left here or is it just duplicate of dotnet/runtime#14753?

@karelz That part of https://github.com/dotnet/corefx/issues/2146 was closed by the question "Is this really common?", with no response given to the people (including me) who think it is common. So maybe its closing could be reconsidered?

(Though that issue is also a mix of unrelated proposals. Keeping this issue open instead might be the better choice.)

Alright, I let the new area owners to comment hen they get to it during their incomming triage - @cston @333fred (cc @jaredpar)

Chunking is a useful feature which we should consider adding to System.Linq. My preferred signature would be the following:

public static class Enumerable
{
     public static IEnumerable<T[]> ChunkBy(this IEnumerable<T> source, int size);
}

F# core already has a corresponding chunkBySize function.

Revisit for .NET 6?

We could take a look. @inputfalken would you be able to update the original post to match the API review template?

Revisit for .NET 6?

We could take a look. @inputfalken would you be able to update the original post to match the API review template?

@eiriktsarpalis

Was this page helpful?
0 / 5 - 0 ratings