-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Enhanced any
and all
.
#1839
Enhanced any
and all
.
#1839
Conversation
how does this look diff --git a/src/builtin.jq b/src/builtin.jq
index d3a4bb1..6105582 100644
--- a/src/builtin.jq
+++ b/src/builtin.jq
@@ -32,20 +32,6 @@ def index($i): indices($i) | .[0]; # TODO: optimize
def rindex($i): indices($i) | .[-1:][0]; # TODO: optimize
def paths: path(recurse(if (type|. == "array" or . == "object") then .[] else empty end))|select(length > 0);
def paths(node_filter): . as $dot|paths|select(. as $p|$dot|getpath($p)|node_filter);
-def any(generator; condition):
- [label $out | foreach generator as $i
- (false;
- if . then break $out elif $i | condition then true else . end;
- if . then . else empty end)] | length == 1;
-def any(condition): any(.[]; condition);
-def any: any(.);
-def all(generator; condition):
- [label $out | foreach generator as $i
- (true;
- if .|not then break $out elif $i | condition then . else false end;
- if .|not then . else empty end)] | length == 0;
-def all(condition): all(.[]; condition);
-def all: all(.);
def isfinite: type == "number" and (isinfinite | not);
def arrays: select(type == "array");
def objects: select(type == "object");
@@ -172,6 +158,12 @@ def limit($n; exp):
elif $n == 0 then empty
else exp end;
def isempty(g): 0 == ((label $go | g | (1, break $go)) // 0);
+def all(generator; condition): isempty(generator|condition and empty);
+def all(condition): all(.[]; condition);
+def all: all(.[]; .);
+def any(generator; condition): all(generator; condition|not)|not;
+def any(condition): all(.[]; condition|not)|not;
+def any: all(.[]; not)|not;
def first(g): label $out | g | ., break $out;
def last(g): reduce g as $item (null; $item);
def nth($n; g): if $n < 0 then error("nth doesn't support negative indices") else last(limit($n + 1; g)) end; Since all/2 is the most natural to express, I defined that first, and defined all the other variants directly in terms of all/2; even the edit: clearly not obviously enough. |
It should also be correct to define |
How much efficient is |
More idiomatic
But slower, because |
... also, the current any/2 and all/2 don't short-circuit correctly, they evaluate one more argument than necessary, as I found trying to add a test
|
(and should |
that's in Since this is not documented or needed, it's clearly a bug, and we should fix it. EDIT: It'd be good if I could remember why I added that case... |
The issue goes deeper than that--jq doesn't halt on
|
@muhmuhten Ahh, because I made it so when a C-coded builtin returns a
That's fine, but
Now, a
So we could just map Alternatively we could use a new Which is preferable? The latter would allow I'm inclined to map |
Culprit looks like 906d253. There are no C-coded builtins that return a jv_invalid() naturally (other than error), though? |
@muhmuhten There are no other C-coded builtins that do that. Of the jq-coded builtins, the only one that needs some thought is
Byte-coded builtins throw no errors. EDIT: That is, all other C-coded builtins always pass a non- |
Re: |
Ah, I see the complexity--an invalid without a message is process()'s signal that there's no more output as well, so we can't just pass it up and handle it as an error, because that would make every program look like it finished with a (null) error? I think having error/0 silently replace an input null with a string violates the assumption that what we get back when catching an error is what went in, and having Maybe as a compromise we can having error/0 error on input null with an error along the lines of "error/0 requires a non-null input" and put it on the list of things to fix in ($far_future = never)... Then remove the backtrack on C returning invalid since apparently it's not needed. |
Let's continue this tomorrow or later this week. I have a bunch of branches lying around with various half-baked attempts at adding I/O-related builtins and co-routines, and who knows what else. I suspect I needed this behavior at some point for C-coded builtins, but if nothing is using it, then indeed, we can just remove this and that's the simplest fix. We can always add a "backtrack" jv pseudo-type for this later. |
The one C-coded builtin that could benefit from a convention for "hey, just backtrack now" is I'll make it so. BTW, I've done a bit of work just now on adding support for shared objects / DLLs in libraries... Not even remotely tested -- what I have builds, but that's it. |
#1845 subsumes this PR. Thanks @fadado and @muhmuhten! |
After the last changes in
builtin.jq
my old pull requests cannot merge automatically. I hopemy enhanced definitions for
any
andall
in this new pull request can be merged soon. The new definitions are:empty
JJOR