-
Notifications
You must be signed in to change notification settings - Fork 668
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
feat(wrapper array): Allow negative indices to be passed to .at() #1244
Conversation
cd8b03e
to
8a02abc
Compare
Thanks for the PR. I haven't seen this in an API before, what are some examples of language features/ libraries that use -1 to mean the end of an array? We could also add a |
@eddyerburgh The best example I can think of is Python, which has this behavior built-in for lists, eg: >>> list = [1, 2, 3]
>>> list[-1]
3
>>> list[-2]
2
>>> list[-3]
1 To me it would feel pretty natural to have this implemented in A
|
@eddyerburgh I hope you don't mind me jumping in here. 😃 A similar API is used in |
This feels a bit unintuitive to me. Since JS doesn't support this natively, I'm not sure it makes sense here, either. |
@lmiller1990 In my comment above I have linked native JavaScript APIs that support negative indices. If you think that this is not the expected behavior, what behavior would you expect when doing |
I would expected undefined: I'm actually surprised to find there is some support for If we want to follow
|
I agree that, when manipulating a native Array in Js, it feels counterintuitive to access a negative index like you would in Python, but doing that through an API (
The goal here is not to mimic But I could imagine a
Again, a |
Passing a negative index to .at() lets you retrieve elements by starting from the end of the wrapper array (i.e. last item is at index -1)
8a02abc
to
996d3e5
Compare
Ok, I reckon we merge this up. Going to get few more reviews going. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Passing a negative index to .at() lets you retrieve elements by starting from the end of the wrapper array (i.e. last item is at index -1).