From 821cd31e67444d60656baf463e4603899697b045 Mon Sep 17 00:00:00 2001 From: Nicolas Williams Date: Sun, 6 Jul 2014 20:08:06 -0500 Subject: [PATCH] Add `any/N` and `all/N` x N in (1, 2) (fix #455) Contributed by @pkoppstein. --- builtin.c | 4 ++++ docs/content/3.manual/manual.yml | 16 ++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/builtin.c b/builtin.c index bda2c9ea..e742c0e2 100644 --- a/builtin.c +++ b/builtin.c @@ -934,6 +934,10 @@ static const char* const jq_builtins[] = { "def paths(node_filter): . as $dot|paths|select(. as $p|$dot|getpath($p)|node_filter);", "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 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 arrays: select(type == \"array\");", "def objects: select(type == \"object\");", "def iterables: arrays, objects;", diff --git a/docs/content/3.manual/manual.yml b/docs/content/3.manual/manual.yml index 54e727b3..4aa4611e 100644 --- a/docs/content/3.manual/manual.yml +++ b/docs/content/3.manual/manual.yml @@ -766,7 +766,7 @@ sections: input: '[]' output: ["null"] - - title: "`any`" + - title: "`any`, `any(condition), `any(generator; condition)`" body: | The filter `any` takes as input an array of boolean values, @@ -775,6 +775,12 @@ sections: If the input is an empty array, `any` returns `false`. + The `any(condition)` form applies the given condition to the + elements of the input array. + + The `any(generator; condition)` form applies the given + condition to all the outputs of the given generator. + examples: - program: any input: '[true, false]' @@ -786,13 +792,19 @@ sections: input: '[]' output: ["false"] - - title: "`all`" + - title: "`all`, `all(condition), `all(generator; condition)`" body: | The filter `all` takes as input an array of boolean values, and produces `true` as output if all of the the elements of the array are `true`. + The `all(condition)` form applies the given condition to the + elements of the input array. + + The `all(generator; condition)` form applies the given + condition to all the outputs of the given generator. + If the input is an empty array, `all` returns `true`. examples: