Skip to content

[API Proposal]: Linq method for getting value and index #95563

Closed

Description

@terrajobst

Background and motivation

There is an existing Linq method for returning an enumerable's value and index, but it was added pre-tuples which makes it clunky as it forces the caller to create a type representation for the combination of the value and index, for example using this:

IEnumerable<string> lines = GetLines();

foreach (var (line, lineIndex) in lines.Select((l, i) => (l, i)))
{
    // ...
}

API Proposal

namespace System.Linq;

public static partial class Enumerable
{
    public static IEnumerable<(T Value, int Index)> WithIndex<T>(this IEnumerable<T> source);
}

public static partial class Queryable
{
    public static IQueryable<(T Value, int Index)> WithIndex<T>(this IQueryable<T> source);
}

API Usage

Using WithIndex, one can express this idea much more succinctly:

IEnumerable<string> lines = GetLines();

foreach (var (line, lineIndex) in lines.WithIndex())
{
    // ...
}

Alternative Designs

No response

Risks

None.

Activity

  1. added
    api-suggestion
    on Dec 4, 2023
  2. ghost added
    untriaged
    on Dec 4, 2023
  3. eiriktsarpalis commented on Dec 4, 2023

    @eiriktsarpalis
    Member

    How about ZipIndex or ZipWithIndex? Select methods typically imply some form of transformation, however this one just zips the source enumerable with its indices (and has a similar output type to Zip).

  4. KeterSCP commented on Dec 4, 2023

    @KeterSCP

    I also want to point out that name SelectIndex is very unintuitive. It implies that you select only index, which is not true. SelectWithIndex or ZipWithIndex are more verbose, but at least they do not introduce confusion

  5. marklio commented on Dec 4, 2023

    @marklio

    How about just WithIndex? I agree that 'Select' implies transformation. 'Zip' implies starting with two things. This is just bringing an implicit attribute of the enumerable to the surface.

    This is an extension that I have in almost all the code I write, so I'd love it if it were just ambiently available.

  6. terrajobst commented on Dec 4, 2023

    @terrajobst
    ContributorAuthor

    @eiriktsarpalis that makes sense. Personally, I'd prefer @marklio's suggestion WithIndex over ZipIndex because it seems easier to understand.

  7. eiriktsarpalis commented on Dec 5, 2023

    @eiriktsarpalis
    Member

    Methods starting with With are typically associated with update expressions e.g. in fluent APIs or records. This is not in the same boat. Marking ready for review, I'm sure more than ample debate will be expended on the naming.

  8. removed
    untriaged
    on Dec 5, 2023
  9. added this to the 9.0.0 milestone on Dec 5, 2023
  10. added
    api-ready-for-review
    and removed
    api-suggestion
    on Dec 5, 2023
  11. aloraman commented on Dec 5, 2023

    @aloraman
    1. How about AsIndexed or just Indexed?
    2. What's the intended meaning of EventualNameOfThisMethod in IQueryable, e.g. should it be translated into ROW_NUMBER()?
  12. viceroypenguin commented on Dec 5, 2023

    @viceroypenguin

    The same method in MoreLinq and SuperLinq is just Index(). This is likely not a good choice for the name, but raising awareness of the community implementations of this.

  13. terrajobst commented on Dec 5, 2023

    @terrajobst
    ContributorAuthor

    I'm sure more than ample debate will be expended on the naming.

  14. y0ung3r commented on Dec 5, 2023

    @y0ung3r

    What about IndexableSelect or IndexedSelect? 😂

  15. terrajobst commented on Dec 12, 2023

    @terrajobst
    ContributorAuthor
    • We decided against WithIndex because it sounds like a wither which this one isn't. F# calls it Indexed; following nomenclature in the rest of Linq we chose a verb version (akin to the recently added Order)
    • We decided to rename Value to Item because Value implies a singleton vs Item which is used as a collection indexer
    • We decided to return Index first, Value second because it feels more natural despite the fact that the existing Select() method provides value first and index second.
      IEnumerable<string> lines = GetLines();
      
      foreach (var (i, line) in lines.Index())
      {
          // ...
      }
    • We should match the existing Select's method behavior when enumerating a source that returns more than int.MaxValue items
    namespace System.Linq;
    
    public static partial class Enumerable
    {
        public static IEnumerable<(int Index, T Item)> Index<T>(this IEnumerable<T> source);
    }
    
    public static partial class Queryable
    {
        public static IQueryable<(int Index, T Item)> Index<T>(this IQueryable<T> source);
    }
  16. added
    api-approved
    and removed
    api-ready-for-review
    on Dec 12, 2023
  17. added
    help wanted
    on Dec 12, 2023
  18. ghost added
    in-pr
    on Dec 13, 2023
  19. ghost removed
    in-pr
    on Dec 13, 2023
  20. locked and limited conversation to collaborators on Jan 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedarea-System.Linqhelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions