You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[API Proposal]: Linq method for getting value and index #95563
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:
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).
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
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.
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.
Reacted by Vlad Zarytovskii, Evgeniy Alexandrov, Immo Landwerth and Cole Tobin
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.
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.
Activity
eiriktsarpalis commentedon Dec 4, 2023
How about
ZipIndexorZipWithIndex?Selectmethods typically imply some form of transformation, however this one just zips the source enumerable with its indices (and has a similar output type toZip).KeterSCP commentedon Dec 4, 2023
I also want to point out that name
SelectIndexis very unintuitive. It implies that you select only index, which is not true.SelectWithIndexorZipWithIndexare more verbose, but at least they do not introduce confusionmarklio commentedon Dec 4, 2023
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.
terrajobst commentedon Dec 4, 2023
@eiriktsarpalis that makes sense. Personally, I'd prefer @marklio's suggestion
WithIndexoverZipIndexbecause it seems easier to understand.eiriktsarpalis commentedon Dec 5, 2023
Methods starting with
Withare 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.aloraman commentedon Dec 5, 2023
AsIndexedor justIndexed?EventualNameOfThisMethodinIQueryable, e.g. should it be translated intoROW_NUMBER()?viceroypenguin commentedon Dec 5, 2023
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.terrajobst commentedon Dec 5, 2023
y0ung3r commentedon Dec 5, 2023
What about
IndexableSelectorIndexedSelect? 😂terrajobst commentedon Dec 12, 2023
WithIndexbecause it sounds like a wither which this one isn't. F# calls itIndexed; following nomenclature in the rest of Linq we chose a verb version (akin to the recently addedOrder)ValuetoItembecauseValueimplies a singleton vsItemwhich is used as a collection indexerIndexfirst,Valuesecond because it feels more natural despite the fact that the existingSelect()method provides value first and index second.Select's method behavior when enumerating a source that returns more thanint.MaxValueitems