jaq icon indicating copy to clipboard operation
jaq copied to clipboard

Add while/until to standard library

Open capezotte opened this issue 3 years ago • 3 comments

This adds the while command from jq's standard library.

$ ./target/debug/jaq '[ while(. < 100; . * 2) ]' <<< 1
[
  1,
  2,
  4,
  8,
  16,
  32,
  64
]

capezotte avatar Aug 05 '22 02:08 capezotte

Fixed a typo that caused one more object to appear in comparison to jq, sorry.

capezotte avatar Aug 05 '22 02:08 capezotte

Thanks a lot for your contribution! Would you mind adding some tests for your new filters to jaq-std/tests/std.rs?

01mf02 avatar Aug 05 '22 15:08 01mf02

Done.

capezotte avatar Aug 06 '22 01:08 capezotte

I found a difference between your implementation and jq's one: until(.; (1, 2)) with input null yields 1 2 with jq's builtin until, whereas with your implementation of until, it yields just 2.

01mf02 avatar Aug 09 '22 07:08 01mf02

Thanks for your tests, by the way. Just to be clear: If you find a nice (read: jq-compliant and performant) way to implement until, then I'll be happy to merge your PR. :) But if not, then I'll be also fine with only merging your while implementation.

01mf02 avatar Aug 10 '22 15:08 01mf02

So far I don't see a way to implement until with just the existing functions without failing tests with commas. Guess I'll drop it.

capezotte avatar Aug 10 '22 17:08 capezotte

Thanks a lot! I felt inspired by your effort and implemented until in 9cda5b086507014d5d48074749d156bda71400f5 as core filter --- it turned out that this was just a slight generalisation of the recurse filter. If you still have your tests for until, I would be happy to incorporate them, perhaps in a new pull request?

01mf02 avatar Aug 11 '22 11:08 01mf02

Ok, I went forward and incorporated your tests for until in c8190bb. Thanks again!

01mf02 avatar Aug 16 '22 10:08 01mf02