Skip to content

Commit 996ba26

Browse files
committed
GROUPS_BY(s;f) with test and documentation.
./jqtest => 307 of 307 tests passed (0 malformed)
1 parent 607bff6 commit 996ba26

File tree

4 files changed

+367
-1
lines changed

4 files changed

+367
-1
lines changed

docs/content/3.manual/manual.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,20 @@ sections:
13121312
input: '[{"foo":1, "bar":10}, {"foo":3, "bar":100}, {"foo":1, "bar":1}]'
13131313
output: ['[[{"foo":1, "bar":10}, {"foo":1, "bar":1}], [{"foo":3, "bar":100}]]']
13141314

1315+
- title: "`GROUPS_BY(stream; path_expression)`"
1316+
body: |
1317+
1318+
`GROUPS_BY/2` is a stream-oriented version of group_by/1: it takes as input the items
1319+
in `stream`, and emits a stream of arrays, one per group as defined by
1320+
`path_expression`, which must evaluate to a string for all items in the stream.
1321+
1322+
examples:
1323+
- program: 'GROUPS_BY(1,2,1; tostring)'
1324+
input: 'null'
1325+
output:
1326+
- ['[1,1]']
1327+
- ['[2]']
1328+
13151329
- title: "`min`, `max`, `min_by(path_exp)`, `max_by(path_exp)`"
13161330
body: |
13171331

src/builtin.jq

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,6 @@ def JOIN($idx; stream; idx_expr; join_expr):
303303
stream | [., $idx[idx_expr]] | join_expr;
304304
def IN(s): . as $in | first( if (s == $in) then true else empty end ) // false;
305305
def IN(src; s): any( src|IN(s); .);
306+
307+
# f should always evaluate to a string:
308+
def GROUPS_BY(stream; f): reduce stream as $x ({}; .[$x|f] += [$x] ) | .[];

tests/jq.test

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1420,4 +1420,7 @@ isempty(1,error("foo"))
14201420
null
14211421
false
14221422

1423-
1423+
GROUPS_BY(1,1,2; tostring)
1424+
null
1425+
[1,1]
1426+
[2]

0 commit comments

Comments
 (0)