-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
proposal: x/exp/slices: add First and Last #53510
Comments
Should |
I would expect so, in the same way that |
I'm not sure if symmetry is enough justification to add a function that basically does nothing of value. I find
perfectly fine. |
Wouldn't it be better to just implement #25594, or a variant thereof?
|
Thanks for highlighting #25594. I like the solution that it proposes more than this proposal, but it seems that similar languages changes like it have been proposed multiple times and rejected. Barring a language change, perhaps a library change is more palatable. I would argue that the frequency that similar proposals keep getting filed indicates that this is a real pain point for users that is sufficiently common. |
Not only is it better for readability, but it also resolves the inability for |
What about |
'At' would almost always only be used with negative indices, because s[i] is clearer for the other cases, so why not a 'FromEnd' function:
|
This proposal has been added to the active column of the proposals project |
This feels like it starts to fight the language. /cc @robpike |
The comment that this is turning an idiom into a function is a good one. The addition of slices.First is unnecessary, more verbose than the idiom, except to complement slices.Last. So is Last necessary? It can help when the slice expression is messy, but many things we do in any programming language get messy when the expression is messy. In other words, it doesn't add functionality, only convenience, and that requires a high bar that Last might not meet. And once we have First and Last, there will be calls for Index and Nth and Last2 and LastN and so on. It's a can you might not want to open. The philosophical question is: do we want to turn the language into this? We could have had maps and slices and so on be run with functions or methods, but we didn't. It would be sad to wake up one day and find many uses of slices eschewing the index notation. |
When you're dealing with a short local variable, the some.Nested.Values[len(some.Nested.Values)-1] vs slices.At(some.Nested.Values, -1) |
@icholy You can always assign a long expression to a short local variable.
|
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
Based on the discussion above, this proposal seems like a likely decline. |
No change in consensus, so declined. |
The pattern
s[len(s)-1]
appears fairly often.However,
s[len(s)-1]
is neither readable nor easy to type,especially if
s
is a relatively complex expression.Thus, I propose the addition of
slices.Last
thatretrieves the last elements, respectively.
I also propose adding
slices.First
for consistency.Supporting evidence
Frequency of constant slice indexes relative to start or end:
Even though obtaining the last element is an order-of-magnitude
less frequent than getting the first element,
there are still ~300k occurences.
Of the number of cases where
s[len(s)-1]
appears,it appears alongside
s[0]
78% of the time.For example:
For this reason, I propose the addition of
slices.First
,so that these case will read naturally:
Considerations
This API does not help the following statement:
since the returned value of
slices.Last
cannot be assigned to.This occurs 32344 times (10.6%).
It also does not help the following expression:
since you cannot take the address of the return value of
slice.Last
.This occurs 8102 times (2.6%).
To support these 13% of cases, we could have
slices.Last
return a pointer to the element, so that you could do:
However, that is clunky to use.
Alternatives
Last
is unhelpful if you want the penultimate element (i.e.,s[len(s)-2]
),but there are an order-of-magnitude fewer use-cases for that compared
to just getting the last element.
We could consider a Python-like indexing operation with negative indexing:
This provides a general-purpose way to retrieve elements relative to the end.
Negative indexing has been proposed many times in the past for slicing and was rejected
because it is easy for programming bug to go unnoticed.
Using an explicit API like
slices.At
could be clear enough signal that we truly wantnegative indexing. Perhaps that is an appropriate trade-off.
I personally would like to have
At
as well,but the evidence does not seem to indicate that it is as prevalent
compared to the need for
Last
.\cc @ianlancetaylor @eliben
The text was updated successfully, but these errors were encountered: