Implement add/1 (#3144)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by myaaaaaaaaa · committed by GitHub · parent 137018d · Browse files · 5 files·+35 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+35 added−4 removed
- tests:+17 added−0 removed
- other:+18 added−4 removed
Provenance
- Commit
0b5ae30- Tree
c03bc08- Author
myaaaaaaaaa <103326468+myaaaaaaaaa@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
137018d
5 files changed+35 added−4 removed
modified:docs/content/manual/dev/manual.yml+7 added-1 removed
1298 unmodified lines1299 - program: '[paths(type == "number")]'1300 input: '[1,[[],{"a":2}]]'1301 output: ['[[0],[1,1,"a"]]']1302 1303- - title: "`add`"1303+ - title: "`add`, `add(generator)`"1304 body: |1305 1306 The filter `add` takes as input an array, and produces as1307 output the elements of the array added together. This might2 unmodified lines1310 as those for the `+` operator (described above).1311 1312 If the input is an empty array, `add` returns `null`.1313 1314+ `add(generator)` operates on the given generator rather than1315+ the input.1316+1317 examples:1318 - program: add1319 input: '["a","b","c"]'1320 output: ['"abc"']2 unmodified lines1323 output: ['6']1324 - program: add1325 input: '[]'1326 output: ["null"]1327+ - program: add(.[].a)1328+ input: '[{"a":3}, {"a":5}, {"b":6}]'1329+ output: ['8']1330 1331 - title: "`any`, `any(condition)`, `any(generator; condition)`"1332 body: |1333 2473 unmodified linesmodified:src/builtin.jq+2 added-1 removed
6 unmodified lines7 def unique: group_by(.) | map(.[0]);8 def unique_by(f): group_by(f) | map(.[0]);9 def max_by(f): _max_by_impl(map([f]));10 def min_by(f): _min_by_impl(map([f]));11-def add: reduce .[] as $x (null; . + $x);11+def add(f): reduce f as $x (null; . + $x);12+def add: add(.[]);13 def del(f): delpaths([path(f)]);14 def abs: if . < 0 then - . else . end;15 def _assign(paths; $value): reduce path(paths) as $p (.; setpath($p; $value));16 def _modify(paths; update):265 unmodified linesmodified:tests/jq.test+13 added
640 unmodified lines641 map_values(.+1)642 [0,1,2]643 [1,2,3]644 645+[add(null), add(range(range(10))), add(empty), add(10,range(10))]646+null647+[null,120,null,55]648+649+# Real-world use case for add(empty)650+.sum = add(.arr[])651+{"arr":[]}652+{"arr":[],"sum":null}653+654+add({(.[]):1}) | keys655+["a","a","b","a","d","b","d","a","d"]656+["a","b","d"]657+658 #659 # User-defined functions660 # Oh god.661 #1530 unmodified linesmodified:tests/man.test+4 added
356 unmodified lines357 add358 []359 null360 361+add(.[].a)362+[{"a":3}, {"a":5}, {"b":6}]363+8364+365 any366 [true, false]367 true368 603 unmodified linesmodified:jq.1.prebuilt+9 added-2 removed
1 .2-.TH "JQ" "1" "May 2024" "" ""2+.TH "JQ" "1" "July 2024" "" ""3 .4 .SH "NAME"5 \fBjq\fR \- Command\-line JSON processor6 .1332 unmodified lines1339 .fi1340 .1341 .IP "" 01342 .1343-.SS "add"1343+.SS "add, add(generator)"1344 The filter \fBadd\fR takes as input an array, and produces as output the elements of the array added together\. This might mean summed, concatenated or merged depending on the types of the elements of the input array \- the rules are the same as those for the \fB+\fR operator (described above)\.1345 .1346 .P1347 If the input is an empty array, \fBadd\fR returns \fBnull\fR\.1348 .1349+.P1350+\fBadd(generator)\fR operates on the given generator rather than the input\.1351+.1352 .IP "" 41353 .1354 .nf1355 7 unmodified lines1363 1364 jq \'add\'1365 []1366 => null1367+1368+jq \'add(\.[]\.a)\'1369+ [{"a":3}, {"a":5}, {"b":6}]1370+=> 81371 .1372 .fi1373 .1374 .IP "" 02868 unmodified lines