Skip to content
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

Closed
mm3509 opened this issue Jul 25, 2018 · 8 comments
Closed

Negative indexing like R #28276

mm3509 opened this issue Jul 25, 2018 · 8 comments

Comments

@mm3509
Copy link

mm3509 commented Jul 25, 2018

I like R's feature of selecting all but a certain element with negative indexing, e.g.

> a = c(1, 2, 3)
> a[-1]
[1] 2 3

In Julia, this thread suggests 1:end .!= 1, which works. I think it could be enhanced with negative indexing.

@JeffBezanson
Copy link
Member

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.

@JeffBezanson
Copy link
Member

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

@JeffBezanson
Copy link
Member

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.

@mbauman
Copy link
Member

mbauman commented Jul 25, 2018

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.

@mbauman mbauman closed this as completed Jul 25, 2018
@mm3509
Copy link
Author

mm3509 commented Jul 26, 2018

Yes, good arguments all around. I searched for "negative" instead of "negated", so I missed #1032. InvertedIndices does exactly what I want. Thanks!

@benatkin
Copy link

benatkin commented Sep 6, 2021

It's arbitrary what Python does, but useful, and has expanded to other languages, the latest being JavaScript.

@timholy
Copy link
Member

timholy commented Sep 8, 2021

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/

@StefanKarpinski
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants