Make any/2 and all/2 efficient using foreach
This commit is contained in:
1 parent
80a9937e48
commit
4bcff40828
2 files changed
+28
-2
No files matched your search
@@ -936,9 +936,19 @@ static const char* const jq_builtins[] = {
|
||||
"def any: reduce .[] as $i (false; . or $i);",
|
||||
"def all: reduce .[] as $i (true; . and $i);",
|
||||
"def any(condition): reduce .[] as $i (false; . or ($i|condition));",
|
||||
"def any(generator; condition):"
|
||||
" [false,"
|
||||
" foreach generator as $i"
|
||||
" (false;"
|
||||
" if . then break elif $i | condition then true else . end;"
|
||||
" if . then . else empty end)] | any;",
|
||||
"def all(condition): reduce .[] as $i (true; . and ($i|condition));",
|
||||
"def any(generator; condition): reduce generator as $i (false; . or ($i|condition));",
|
||||
"def all(generator; condition): reduce generator as $i (true; . and ($i|condition));",
|
||||
"def all(generator; condition): "
|
||||
" [true,"
|
||||
" foreach generator as $i"
|
||||
" (true;"
|
||||
" if .|not then break elif $i | condition then . else false end;"
|
||||
" if .|not then . else empty end)]|all;",
|
||||
"def arrays: select(type == \"array\");",
|
||||
"def objects: select(type == \"object\");",
|
||||
"def iterables: arrays, objects;",
|
||||
|
||||
@@ -502,6 +502,22 @@ reduce .[] as $x (0; . + $x)
|
||||
[1,2,4]
|
||||
7
|
||||
|
||||
. as $dot|any($dot[];not)
|
||||
[1,2,3,4,true,false,1,2,3,4,5]
|
||||
true
|
||||
|
||||
. as $dot|any($dot[];not)
|
||||
[1,2,3,4,true]
|
||||
false
|
||||
|
||||
. as $dot|all($dot[];.)
|
||||
[1,2,3,4,true,false,1,2,3,4,5]
|
||||
false
|
||||
|
||||
. as $dot|all($dot[];.)
|
||||
[1,2,3,4,true]
|
||||
true
|
||||
|
||||
#
|
||||
# Paths
|
||||
#
|
||||
|
||||
Reference in new issue
Block a user