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

feat: allow negative index/end on std.slice #1093

Merged
merged 3 commits into from
Mar 2, 2024

Conversation

Duologic
Copy link
Contributor

@Duologic Duologic commented Jun 1, 2023

I found myself implementing this elsewhere and figured it'd be easy to support in stdlib.

I ran make test, it succeeded for my changes but had unrelated failures, we might want to fix that in a separate PR.

@sparkprime
Copy link
Contributor

@sbarzowski Will this "just work" on go-jsonnet?

I think I was originally opinionated about not supporting this as I wanted the language to be simple... But in retrospect it seems more confusing to support python slicing only "partially" so this seems a good idea.

@sbarzowski
Copy link
Collaborator

sbarzowski commented Jun 13, 2023

Will this "just work" on go-jsonnet?

Yes. I think we just desugar to std.slice. That said ideally we would add a builtin (but that's neither easier nor harder with this change in).

BTW this change definitely requires an update to documentation.

CertainLach added a commit to CertainLach/jrsonnet that referenced this pull request Jun 14, 2023
@johnbartholomew
Copy link
Collaborator

It seems like this was almost ready to go back in June, with only a request for documentation updates as a possible blocker. @Duologic would you like to contribute some doc changes for this, or should I work on it?

@sparkprime
I'm not sure if there is any coordination needed with other jsonnet implementations to get this out? I assume we merge it here first, then go-jsonnet can pick it up (probably with the next release?) and other implementations (e.g., jrsonnet - which has a PR queued already, CertainLach/jrsonnet#117) can update at their leisure.

@Duologic
Copy link
Contributor Author

I just scanned the documentation and didn't find any mention that negative slices are not supported, am I missing something?

Do you want an example added maybe?

@johnbartholomew
Copy link
Collaborator

johnbartholomew commented Feb 12, 2024

Hmm... it's a good point, the tutorial says "Array slices like arr[10:20:2] are allowed, like in Python", so perhaps people have always assumed negative slices worked :-)

IMHO, yes, it would be good to add an example of a negative index in the std.slice documentation in the standard library reference.

Copy link
Collaborator

@johnbartholomew johnbartholomew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're still willing to work on this PR, could you fix the way inval.index is calculated, so that the behaviour of ("012345")[-7:] matches Python. And add string and array test cases for that in test_suite/slice.sugar.jsonnet. (See my inline code comment)

I realise it's been a while since this PR was first opened, so your motivation or priorities may have changed. If so, please let me know and I'm happy to do the remaining parts and get this merged.

stdlib/std.jsonnet Outdated Show resolved Hide resolved
Duologic and others added 3 commits March 2, 2024 17:40
…fmt test golden

Slice is documented to behave similarly to Python. Negative indexes in
Python slices are clamped so slice indexes never produce out-of-range
runtime errors. Update the 'slice' sugar to do the same.

Note that only the starting index needs to be clamped in this way; the
ending index can be out-of-range doesn't cause an error, as the loop
exit condition compares the current index against the ending index.
@johnbartholomew johnbartholomew merged commit 8c53f3c into google:master Mar 2, 2024
6 checks passed
@johnbartholomew
Copy link
Collaborator

Rebased and merged. :-)

CertainLach added a commit to CertainLach/jrsonnet that referenced this pull request Mar 3, 2024
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

Successfully merging this pull request may close these issues.

4 participants