1
0
mirror of https://github.com/stedolan/jq.git synced 2024-05-11 05:55:39 +00:00

Make any/2 and all/2 efficient using foreach

This commit is contained in:
Nicolas Williams
2014-07-07 22:25:34 -05:00
parent 80a9937e48
commit 4bcff40828
2 changed files with 28 additions and 2 deletions

View File

@ -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;",

View File

@ -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
#