Skip to content

Commit dde43f7

Browse files
Santiago Laprestanicowilliams
authored andcommitted
Simplified standard library
Close #426. Signed-off-by: Nicolas Williams <[email protected]>
1 parent 7ee3b72 commit dde43f7

File tree

3 files changed

+94
-69
lines changed

3 files changed

+94
-69
lines changed

builtin.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -693,19 +693,25 @@ static block bind_bytecoded_builtins(block b) {
693693
static const char* const jq_builtins[] = {
694694
"def map(f): [.[] | f];",
695695
"def select(f): if f then . else empty end;",
696-
"def sort_by(f): _sort_by_impl(map([f]));",
697-
"def group_by(f): _group_by_impl(map([f]));",
698-
"def unique: group_by(.) | map(.[0]);",
699-
"def unique_by(f): group_by(f) | map(.[0]);",
700-
"def max_by(f): _max_by_impl(map([f]));",
701-
"def min_by(f): _min_by_impl(map([f]));",
696+
"def sort(f): _sort_by_impl(map([f]));",
697+
"def sort_by(f): sort(f);",
698+
"def group(f): _group_by_impl(map([f]));",
699+
"def group_by(f): group(f);",
700+
"def unique: group(.) | map(.[0]);",
701+
"def unique(f): group(f) | map(.[0]);",
702+
"def unique_by(f): unique(f);",
703+
"def max(f): _max_by_impl(map([f]));",
704+
"def min(f): _min_by_impl(map([f]));",
705+
"def max_by(f): max(f);",
706+
"def min_by(f): min(f);",
702707
#include "libm.h"
703708
"def add: reduce .[] as $x (null; . + $x);",
704709
"def del(f): delpaths([path(f)]);",
705710
"def _assign(paths; value): value as $v | reduce path(paths) as $p (.; setpath($p; $v));",
706711
"def _modify(paths; update): reduce path(paths) as $p (.; setpath($p; getpath($p) | update));",
707712
"def recurse(f): ., (f | select(. != null) | recurse(f));",
708-
"def recurse_down: recurse(.[]?);",
713+
"def recurse: recurse(.[]?);",
714+
"def recurse_down: recurse;",
709715
"def to_entries: [keys[] as $k | {key: $k, value: .[$k]}];",
710716
"def from_entries: map({(.key): .value}) | add | .//={};",
711717
"def with_entries(f): to_entries | map(f) | from_entries;",
@@ -714,7 +720,7 @@ static const char* const jq_builtins[] = {
714720
"def index(i): if type == \"array\" and (i|type) == \"array\" then .[i] elif type == \"array\" then .[[i]] else .[i] end | .[0];",
715721
"def rindex(i): if type == \"array\" and (i|type) == \"array\" then .[i] elif type == \"array\" then .[[i]] else .[i] end | .[-1:][0];",
716722
"def paths: path(recurse(if (type|. == \"array\" or . == \"object\") then .[] else empty end))|select(length > 0);",
717-
"def leaf_paths: . as $dot|paths|select(. as $p|$dot|getpath($p)|type|. != \"array\" and . != \"object\");",
723+
"def paths(node_filter): . as $dot|paths|select(. as $p|$dot|getpath($p)|node_filter);",
718724
"def any: reduce .[] as $i (false; . or $i);",
719725
"def all: reduce .[] as $i (true; . and $i);",
720726
"def arrays: select(type == \"array\");",
@@ -726,9 +732,11 @@ static const char* const jq_builtins[] = {
726732
"def nulls: select(type == \"null\");",
727733
"def values: arrays, objects, booleans, numbers, strings;",
728734
"def scalars: select(. == null or . == true or . == false or type == \"number\" or type == \"string\");",
735+
"def leaf_paths: paths(scalars);",
729736
"def join(x): reduce .[] as $i (\"\"; . + (if . == \"\" then $i else x + $i end));",
730737
"def flatten: reduce .[] as $i ([]; if $i | type == \"array\" then . + ($i | flatten) else . + [$i] end);",
731738
"def flatten(x): reduce .[] as $i ([]; if $i | type == \"array\" and x > 0 then . + ($i | flatten(x-1)) else . + [$i] end);",
739+
"def range(x): range(0;x);",
732740
};
733741
#undef LIBM_DD
734742

docs/content/3.manual/manual.yml

Lines changed: 77 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -708,29 +708,25 @@ sections:
708708
input: '[1,2,3]'
709709
output: ['[2,3,4]']
710710

711-
- title: "`paths`"
711+
- title: "`paths`, `paths(node_filter)`, `leaf_paths`"
712712
body: |
713713
714-
Outputs the paths to all the elements in its input (except it
715-
does not output the empty list, representing . itself).
716-
717-
`paths` is equivalent to
718-
719-
def paths: path(recurse(if (type|. == "array" or . == "object") then .[] else empty end))|select(length > 0);
714+
`paths` outputs the paths to all the elements in its input
715+
(except it does not output the empty list, representing .
716+
itself).
717+
718+
`paths(f)` outputs the paths to any values for which `f` is true.
719+
That is, `paths(numbers)` outputs the paths to all numeric
720+
values.
720721
722+
`leaf_paths` is an alias of `paths(scalars)`; `leaf_paths` is
723+
*deprecated* and will be removed in the next major release.
724+
721725
examples:
722726
- program: '[paths]'
723727
input: '[1,[[],{"a":2}]]'
724728
output: ['[[0],[1],[1,0],[1,1],[1,1,"a"]]']
725-
726-
- title: "`leaf_paths`"
727-
body: |
728-
729-
Outputs the paths to all the leaves (non-array, non-object
730-
elements) in its input.
731-
732-
examples:
733-
- program: '[leaf_paths]'
729+
- program: '[paths(scalars)]'
734730
input: '[1,[[],{"a":2}]]'
735731
output: ['[[0],[1,1,"a"]]']
736732

@@ -818,13 +814,15 @@ sections:
818814
input: '[{"foo": "bar"}, [{"foo": "baz"}]]'
819815
output: ['[{"foo": "bar"}, {"foo": "baz"}]']
820816

821-
- title: "`range`"
817+
- title: "`range(upto), `range(from;upto)`"
822818
body: |
823819
824820
The `range` function produces a range of numbers. `range(4;10)`
825821
produces 6 numbers, from 4 (inclusive) to 10 (exclusive). The numbers
826822
are produced as separate outputs. Use `[range(4;10)]` to get a range as
827823
an array.
824+
825+
Its first argument can be omitted; it defaults to zero.
828826
829827
examples:
830828
- program: 'range(2;4)'
@@ -833,6 +831,9 @@ sections:
833831
- program: '[range(2;4)]'
834832
input: 'null'
835833
output: ['[2,3]']
834+
- program: '[range(4)]'
835+
input: 'null'
836+
output: ['[0,1,2,3]']
836837

837838
- title: "`floor`"
838839
body: |
@@ -890,7 +891,7 @@ sections:
890891
input: '[0, false, [], {}, null, "hello"]'
891892
output: ['["number", "boolean", "array", "object", "null", "string"]']
892893

893-
- title: "`sort, sort_by`"
894+
- title: "`sort, sort(path_expression), sort_by(path_expression)`"
894895
body: |
895896
896897
The `sort` functions sorts its input, which must be an
@@ -909,82 +910,90 @@ sections:
909910
sorted order), and if their keys are equal then the values
910911
are compared key by key.
911912
912-
`sort_by` may be used to sort by a particular field of an
913-
object, or by applying any jq filter. `sort_by(foo)`
914-
compares two elements by comparing the result of `foo` on
915-
each element.
913+
`sort` may be used to sort by a particular field of an
914+
object, or by applying any jq filter.
915+
916+
`sort(foo)` compares two elements by comparing the result of
917+
`foo` on each element.
918+
919+
`sort_by(foo)` is an alias of `sort(foo)`; `sort_by()` is
920+
*deprecated* and will be removed in the next major release.
916921
917922
examples:
918923
- program: 'sort'
919924
input: '[8,3,null,6]'
920925
output: ['[null,3,6,8]']
921-
- program: 'sort_by(.foo)'
926+
- program: 'sort(.foo)'
922927
input: '[{"foo":4, "bar":10}, {"foo":3, "bar":100}, {"foo":2, "bar":1}]'
923928
output: ['[{"foo":2, "bar":1}, {"foo":3, "bar":100}, {"foo":4, "bar":10}]']
924929

925-
- title: "`group_by`"
930+
- title: "`group(path_expression)`, `group_by(path_expression)`"
926931
body: |
927932
928-
`group_by(.foo)` takes as input an array, groups the
933+
`group(.foo)` takes as input an array, groups the
929934
elements having the same `.foo` field into separate arrays,
930935
and produces all of these arrays as elements of a larger
931936
array, sorted by the value of the `.foo` field.
932937
933938
Any jq expression, not just a field access, may be used in
934939
place of `.foo`. The sorting order is the same as described
935940
in the `sort` function above.
936-
941+
942+
`group_by(foo)` is an alias of `group(foo)`; `group_by()` is
943+
*deprecated* and will be removed in the next major release.
944+
937945
examples:
938-
- program: 'group_by(.foo)'
946+
- program: 'group(.foo)'
939947
input: '[{"foo":1, "bar":10}, {"foo":3, "bar":100}, {"foo":1, "bar":1}]'
940948
output: ['[[{"foo":1, "bar":10}, {"foo":1, "bar":1}], [{"foo":3, "bar":100}]]']
941949

942-
- title: "`min`, `max`, `min_by`, `max_by`"
950+
- title: "`min`, `max`, `min(path_exp)`, `max(path_exp)`, `min_by(path_exp)`, `max_by(path_exp)`"
943951
body: |
944952
945-
Find the minimum or maximum element of the input array. The
946-
`_by` versions allow you to specify a particular field or
947-
property to examine, e.g. `min_by(.foo)` finds the object
953+
Find the minimum or maximum element of the input array.
954+
This filter accepts an optional argument that
955+
allows you to specify a particular field or
956+
property to examine, e.g. `min(.foo)` finds the object
948957
with the smallest `foo` field.
958+
959+
For legacy reasons, `min_by(.foo)` and `max_by(.foo)` exist as
960+
aliases for `min(.foo)` and `max(.foo)`. These aliases are
961+
considered *deprecated* and will be removed in the next major
962+
release.
949963
950964
examples:
951965
- program: 'min'
952966
input: '[5,4,2,7]'
953967
output: ['2']
954-
- program: 'max_by(.foo)'
968+
- program: 'max(.foo)'
955969
input: '[{"foo":1, "bar":14}, {"foo":2, "bar":3}]'
956970
output: ['{"foo":2, "bar":3}']
957971

958-
- title: "`unique`"
972+
- title: "`unique`, `unique(path_exp)`, `unique_by(path_exp)`"
959973
body: |
960974
961975
The `unique` function takes as input an array and produces
962976
an array of the same elements, in sorted order, with
963-
duplicates removed.
977+
duplicates removed. If an optional argument is passed, it
978+
will keep only one element for each value obtained by applying
979+
the argument. Think of it as making an array by taking one
980+
element out of every group produced by `group`.
981+
982+
For legacy reasons, `unique_by(.foo)` exists as an alias for
983+
`unique(.foo)`. This alias is considered *deprecated* and will
984+
be removed in the next major release.
964985
965986
examples:
966987
- program: 'unique'
967988
input: '[1,2,5,3,5,3,1,3]'
968989
output: ['[1,2,3,5]']
969-
970-
- title: "`unique_by`"
971-
body: |
972-
973-
The `unique_by(.foo)` function takes as input an array and produces
974-
an array of the same elements, in sorted order, with
975-
elqements with a duplicate `.foo` field removed. Think of it as making
976-
an array by taking one element out of every group produced by
977-
`group_by`.
978-
979-
examples:
980-
- program: 'unique_by(.foo)'
990+
- program: 'unique(.foo)'
981991
input: '[{"foo": 1, "bar": 2}, {"foo": 1, "bar": 3}, {"foo": 4, "bar": 5}]'
982992
output: ['[{"foo": 1, "bar": 2}, {"foo": 4, "bar": 5}]']
983-
- program: 'unique_by(length)'
993+
- program: 'unique(length)'
984994
input: '["chunky", "bacon", "kitten", "cicada", "asparagus"]'
985995
output: ['["chunky", "bacon", "asparagus"]']
986996

987-
988997
- title: "`reverse`"
989998
body: |
990999
@@ -1144,10 +1153,10 @@ sections:
11441153
output: ['"a, b,c,d, e"']
11451154

11461155

1147-
- title: "`recurse`"
1156+
- title: "`recurse(f)`, `recurse`, `recurse_down`"
11481157
body: |
11491158
1150-
The `recurse` function allows you to search through a
1159+
The `recurse(f)` function allows you to search through a
11511160
recursive structure, and extract interesting data from all
11521161
levels. Suppose your input represents a filesystem:
11531162
@@ -1165,6 +1174,13 @@ sections:
11651174
with:
11661175
11671176
recurse(.children[]) | .name
1177+
1178+
When called without an argument, `recurse` is equivalent to
1179+
`recurse(.[]?)`.
1180+
1181+
For legacy reasons, `recurse_down` exists as an alias to
1182+
calling `recurse` without arguments. This alias is considered
1183+
*deprecated* and will be removed in the next major release.
11681184
11691185
examples:
11701186
- program: 'recurse(.foo[])'
@@ -1174,20 +1190,21 @@ sections:
11741190
- '{"foo":[]}'
11751191
- '{"foo":[{"foo":[]}]}'
11761192
- '{"foo":[]}'
1177-
1178-
- title: "`recurse_down`"
1179-
body: |
1180-
1181-
A quieter version of `recurse(.[])`, equivalent to:
1182-
1183-
def recurse_down: recurse(.[]?);
1193+
- program: 'recurse'
1194+
input: '{"a":0,"b":[1]}'
1195+
output:
1196+
- '0'
1197+
- '[1]'
1198+
- '1'
11841199

11851200
- title: "`..`"
11861201
body: |
11871202
1188-
Short-hand for `recurse_down`. This is intended to resemble
1189-
the XPath `//` operator. Note that `..a` does not work; use
1190-
`..|a` instead.
1203+
Short-hand for `recurse` without arguments. This is intended
1204+
to resemble the XPath `//` operator. Note that `..a` does not
1205+
work; use `..|a` instead. In the example below we use
1206+
`..|.a?` to find all the values of object keys "a" in any
1207+
object found "below" `.`.
11911208
11921209
examples:
11931210
- program: '..|.a?'

parser.y

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ Term:
477477
$$ = gen_noop();
478478
} |
479479
REC {
480-
$$ = gen_call("recurse_down", gen_noop());
480+
$$ = gen_call("recurse", gen_noop());
481481
} |
482482
Term FIELD '?' {
483483
$$ = gen_index_opt($1, gen_const($2));

0 commit comments

Comments
 (0)