-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Negative indexing like R #28276
Comments
We are not going to add this. If you intend to do normal indexing but an index is negative due to a bug, the bug is hidden. In general, we don't want to do a completely different operation based on an integer having a different value. |
I'll also add that this is especially confusing since python interprets negative indexing as indexing from the end (which we also aren't going to implement, but it helps demonstrate how arbitrary this behavior is). |
Yet another reason we can't implement this is that julia supports arrays with any index offset, so -1 can also be a valid index. These features are simply incompatible, and I believe arbitrary index offsets are much more useful. |
We previously discussed this in #1032. As Jeff noted, we simply cannot support negative numbers for this due to offset arrays and the desire for bounds errors. We can use another special type for this: see the InvertedIndices package as one way to do this. |
Yes, good arguments all around. I searched for "negative" instead of "negated", so I missed #1032. InvertedIndices does exactly what I want. Thanks! |
It's arbitrary what Python does, but useful, and has expanded to other languages, the latest being JavaScript. |
And it would completely break some things that Julia can do that those other languages will never be able to do: https://julialang.org/blog/2017/04/offset-arrays/ |
Also just a horrible, bad idea from a language design perspective (for the reasons given in 2018, which are no less valid today). Just because Python, R and JavaScript thought this was a good idea doesn't mean it is. |
I like R's feature of selecting all but a certain element with negative indexing, e.g.
In Julia, this thread suggests
1:end .!= 1
, which works. I think it could be enhanced with negative indexing.The text was updated successfully, but these errors were encountered: