Implement updates with folding operators (reduce/foreach) on LHS.#281
Merged
Implement updates with folding operators (reduce/foreach) on LHS.#281
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR enables updates with folding operators (
reduceandforeach) on the left-hand side of updates.This is something that jq cannot currently do.
Motivation
The motivating case for this PR was the implementation of
getpath. In jq, we can usegetpathon the left-hand side of updates:In jq,
getpathis a builtin filter implemented in C.In jaq, I tried to first implement
getpathas definition, as such:This is a more convoluted version of the following:
The difference between the two versions is that the first version can be used on the left-hand side of updates, whereas the second version cannot:
Unfortunately, while the first version can be used on the left-hand side of updates, it is significantly slower than the second version.
I then tried to implement
getpathas native filter, like it is done in jq, but this got quite convoluted and required a breaking API change for achieving optimal performance (3eede6c).This got me thinking: How about finally implementing update support for
reduce/foreach? That way, we could use the more performant & simpler second definition ofgetpathand use it on the left-hand side of updates.With this PR, jaq can do it:
Implementation
This is a relatively straightforward implementation of what I described already one year ago in my formal specification of the jq language (section "Update semantics" > "Folding").
Acknowledgements
Thanks to @osevill for having provided a convincing use case for
getpath#277. This greatly motivated me to finally implement this functionality that was already a long time on my roadmap.