01mf02 / jaq

A jq clone focussed on correctness, speed, and simplicity

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

create `jaq_std::Definitions` which includes std definitions, similar to `jaq_core::Definitions`

award28 opened this issue · comments

First off, thanks for this project!

Some of the functionality which is currently behind the jaq binary crate are copied/pasted by users to recreate the functionality. Given this, it seems like some of these methods should be moved to the jaq_std library crate. One such method would be the parse method, which includes the std library definitions as well as the core definitions.

I misunderstood what I needed to do in order to get std definitions integrated with the core definitions. It's pretty straightforward, but I still think something akin to the jaq_core::Definitions could be helpful to have in the std library for anyone who wants to use jaq in code rather than the CLI.

I am currently overhauling the filter definition API as a part of supporting recursive and nested definitions, so I will try whether I can make adding the standard library nicer.

@01mf02 thank you! If you would there are any issues you would like help with, I'm happy to contribute.

I have now changed the API so that you can do:

defs.insert_defs(jaq_std::std(), &mut errs);

This should be easier to use.

Currently, the biggest help would be to torture jaq with recursive and nested filters, because I just implemented some support for this, which is still rough around the corners. If you find some recursive/nested filters that do not work as expected, filing an issue would be a big help. Or even if you have some interesting filters that do work, submitting tests for these would be good as well. af64f3e shows a few tests in a new format that I introduced today which should make it easier to write new tests.

the biggest help would be to torture jaq with recursive and nested filters

First, thank you for you work on this so far. Very impressive.

Second, in accordance with your request above, I revised the jq program in the "Ramsey's theorem" page at rosettacode.org so that it now runs under jaq (or at least b135078), with the minor exception of having to fiddle with the line that uses string interpolation. However the result is wrong.

Please also consider enhancing jaq so that the string interpolation annoyance goes away.

the biggest help would be to torture jaq with recursive and nested filters

Below is a straightforward def of Ackermann's function, with
a "debug" statement to keep track of the recursion depth.
Using jq or gojq, ack(2,2) produces 27 DEBUG lines, along with the correct answer.

jaq, by contrast, goes in circles, and eventually ends with

thread 'main' has overflowed its stack
fatal runtime error: stack overflow

Note that splitting the ack($m-1; ack($m; $n-1)) line into two steps does not help.

def ack($m;$n):
  ([$m,$n] | debug) as $d |
  if $m == 0 then $n + 1
  elif $n == 0 then ack($m-1; 1)
  else ack($m-1; ack($m; $n-1))
  end ;

Thanks for your Ackermann example. That's precisely the kind of thing I need.

It seems that there is a problem when we have multiple variable arguments in a recursive filter --- they get mashed up.
A smaller example that shows the problem is:

def f($a; $b): [$a, $b], f($a+1; $b+1); limit(3; f(1; 2))

It should give:

[1,2]
[2,3]
[3,4]

But it gives:

[2,2]
[3,3]
[4,4]

Should be probably not so hard to fix ...

@pkoppstein, I found and squashed two bugs in the implementation of recursive functions in jaq, and now the Ackermann function yields correct values.

First, thank you for you work on this so far. Very impressive.

Thank you! :)

Second, in accordance with your request above, I revised the jq program in the "Ramsey's theorem" page at rosettacode.org so that it now runs under jaq (or at least b135078), with the minor exception of having to fiddle with the line that uses string interpolation. However the result is wrong.

My bug squashing effort has made it work as well. At least the version without string interpolation.

Please also consider enhancing jaq so that the string interpolation annoyance goes away.

I do not feel motivated enough to work on this feature. On the other hand, if somebody wishes to implement it, I would be happy to accept a PR.

@01fm02 wrote:

My bug squashing effort has made it work as well.

Fantastic! I've updated the Ramsey Theorem and Ackermann function pages on
rosettacode.org to reflect these latest enhancements.

It would really help to have a version number attached to a version
of jaq which handles Ackermann's function correctly :-)

Fantastic! I've updated the Ramsey Theorem and Ackermann function pages on
rosettacode.org to reflect these latest enhancements.

Thank you! Could you perhaps just convert the string interpolation to string concatenation on the Ramsey Theorem so that the example works with all current jq implementations?

-   | "Totally \($cs)connected group: " + (.idx | map(tostring) | join(" "))
+   | "Totally " + $cs + "connected group: " + (.idx | map(tostring) | join(" "))

It would really help to have a version number attached to a version
of jaq which handles Ackermann's function correctly :-)

This should be now the case with jaq 1.0.0-alpha. :)