-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IEnumerable should have a extension for creating fixed size chunks #27449
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 #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.) |
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 |
There has been some discussion about chunking in #14753 (search to find the relevant places). There are a few use cases detailed there. Stack Overflow shows high demand for this API: Revisit for .NET 6? @karelz |
We could take a look. @inputfalken would you be able to update the original post to match the API review template? |
|
I think |
Sounds fine to me, it would be unified with the Take method. |
MoreLINQ includes this method named as Batch: |
namespace System.Linq
{
public static class Enumerable
{
public static IEnumerable<T[]> Chunk(this IEnumerable<T> source, int size);
}
} |
This should have a queryable counterpart, I've updated the proposed API. |
namespace System.Linq
{
public static class Enumerable
{
public static IEnumerable<T[]> Chunk(this IEnumerable<T> source, int size);
}
public static class Queryable
{
public static IQueryable<T[]> Chunk(this IQueryable<T> source, int size);
}
} |
@eiriktsarpalis I would like to grab this if possible. 🙂 Would this be the phase where a pull request is created? |
@inputfalken absolutely, happy to accept PRs for this work |
* Add System.Linq Chunk extension method Fix #27449 * Use explicit types instead of type inference * Seperate inner and outer loop * Rename parameter maxSize to size * Add missing license header * Remove Chunk.SpeedOpt.cs * Add tests to verify Chunk works after mutations * Add/remove before getting enumerator in tests * Test content of chunk method for IQueryable<T>
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 haveSystem.Linq
offer this functionalityProposed API
Usage Examples
Alternative Designs
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.
Risks
This could potentially be a breaking change for users who have their own extension methods due to naming collisions.
The text was updated successfully, but these errors were encountered: