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 index in array.remove off by one error? #1219

Closed
narimiran opened this issue Jul 12, 2023 · 12 comments
Closed

Negative index in array.remove off by one error? #1219

narimiran opened this issue Jul 12, 2023 · 12 comments

Comments

@narimiran
Copy link

Is this intended? (Because definitely it isn't expected)

(array/remove @[10 20 30 40 50] -1) # doesn't remove anything
(array/remove @[10 20 30 40 50] -2) # removes last element

This + 1 in array.c looks suspicious:

at = array->count + at + 1;

@czkz
Copy link
Contributor

czkz commented Jul 12, 2023

Similar behavior with slice:

(slice [10 20 30 40 50] -1) # => ()
(slice [10 20 30 40 50] -2) # => (50)

@pepe
Copy link
Member

pepe commented Jul 12, 2023

Check the doc here https://janet-lang.org/api/array.html#array/slice please

@sogaiu
Copy link
Contributor

sogaiu commented Jul 12, 2023

I think what @pepe might have been hinting at is that this is expected behavior. The negative index convention in Janet might be different from what one might expect from elsewhere but glancing at what has been posted above, they look consistent with what I'm used to.

See here for some examples.

@narimiran
Copy link
Author

Check the doc here https://janet-lang.org/api/array.html#array/slice please

Ok, it seems it is intended. But then it should be mentioned and emphasized in the documentation of every function that uses that convention, because Janet is the only language I know to use -2 for the last element, and I'm sure I'm not the only one who was surprised with it.

@primo-ppcg
Copy link
Contributor

primo-ppcg commented Jul 13, 2023

I'm going to throw my hat in the ring, and say that the behavior of array/remove is probably wrong, or at very least doesn't follow the principle of least surprise.

On the other hand, I think that the behavior of array/slice is correct, however the docstring could, and probably should be reworded:

- Note that index -1 is synonymous with index `(length arrtup)` to allow a full negative slice range.
+ Note that if the range is negative, it is taken as (start, end] to allow a full negative slice range.

This defines the same behavior, without the need to define index -1 to be "outside" the array, which doesn't make sense for any other function.

@narimiran
Copy link
Author

narimiran commented Jul 13, 2023

Check the doc here https://janet-lang.org/api/array.html#array/slice please

Now that I thought about it a bit more — I don't buy it.

Slice can be used without end, so there's no need for this "off by one" so that -1 is the length of the array and not the last element (which most of people/newcomers would expect).

(array/slice @[10 20 30 40 50] 1)
(array/slice @[10 20 30 40 50] -2)

No need to do:

(array/slice @[10 20 30 40 50] 1 -1)
(array/slice @[10 20 30 40 50] -2 -1)

@pepe
Copy link
Member

pepe commented Jul 13, 2023

@narimiran, you do not need to, indeed. But it is like that, and I do not think it will be changed.

@pepe
Copy link
Member

pepe commented Jul 13, 2023

Just some food for thought: you are not always writing number literals by hand as arguments of the functions :-).

@narimiran
Copy link
Author

Just some food for thought: you are not always writing number literals by hand as arguments of the functions :-).

I don't see how that changes the fact that using -2 for the last element is, to put it mildly, surprising.

@pepe
Copy link
Member

pepe commented Jul 14, 2023

There are only two challenging problems in computer science: naming, cache invalidation, and +-1 problem :-).

@sogaiu
Copy link
Contributor

sogaiu commented Jul 16, 2023

@narimiran Do you think #1224 addressed this issue?

If so, may be this issue can be closed.

@narimiran
Copy link
Author

Do you think #1224 addressed this issue?

Yep. Closing this.

Thanks @primo-ppcg!

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

5 participants