Skip to content

IEnumerable should have a extension for creating fixed size chunks #27449

Closed

Description

@inputfalken

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[]> Chunk(this IEnumerable<T> source, int size);
    }

    public static class Queryable
    {
+        public static IQueryable<T[]> Chunk(this IQueryable<T> source, int size);
    }
}

Usage Examples

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

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.

// 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.

Activity

  1. benaadams commented on Sep 22, 2018

    @benaadams
    Member

    GroupBy?

  2. svick commented on Sep 22, 2018

    @svick
    Member

    @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.

  3. inputfalken commented on Sep 22, 2018

    @inputfalken
    ContributorAuthor

    @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.

  4. svick commented on Sep 22, 2018

    @svick
    Member

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

  5. inputfalken commented on Sep 22, 2018

    @inputfalken
    ContributorAuthor

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

  6. svick commented on Sep 22, 2018

    @svick
    Member

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

  7. changed the title [-]IEnumerable should have a extension for creating chunks[/-] [+]IEnumerable should have a extension for creating fixed size chunks[/+] on Sep 22, 2018
  8. karelz commented on Oct 1, 2018

    @karelz
    Member

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

  9. svick commented on Oct 1, 2018

    @svick
    Member

    @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.)

  10. karelz commented on Oct 1, 2018

    @karelz
    Member

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

  11. transferred this issue fromdotnet/corefxon Jan 31, 2020
  12. added this to the Future milestone on Jan 31, 2020
  13. added
    untriaged
    on Feb 23, 2020
  14. removed
    untriaged
    on Sep 2, 2020
  15. eiriktsarpalis commented on Nov 13, 2020

    @eiriktsarpalis
    Member

    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.

  16. 18 remaining items

  17. modified the milestones: Future, 6.0.0 on Jan 28, 2021
  18. removed their assignment
    on Jan 28, 2021
  19. added
    help wanted
    on Jan 28, 2021
  20. inputfalken commented on Jan 28, 2021

    @inputfalken
    ContributorAuthor

    @eiriktsarpalis I would like to grab this if possible. 🙂

    Would this be the phase where a pull request is created?

  21. eiriktsarpalis commented on Jan 28, 2021

    @eiriktsarpalis
    Member

    @inputfalken absolutely, happy to accept PRs for this work ☺️

  22. ghost added
    in-pr
    on Feb 6, 2021
  23. added a commit that references this issue on Feb 10, 2021
    515bfaf
  24. ghost removed
    in-pr
    on Feb 10, 2021
  25. ghost locked as resolved and limited conversation to collaborators on Mar 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Linqhelp wanted

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions