Skip to content
Myr

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

5 files+35 added−4 removed

modified:docs/content/manual/dev/manual.yml+7 added-1 removed
1298 unmodified lines
1299 - program: '[paths(type == "number")]'1300 input: '[1,[[],{"a":2}]]'1301 output: ['[[0],[1,1,"a"]]']13021303 - title: "`add`"1303 - title: "`add`, `add(generator)`"1304 body: |13051306 The filter `add` takes as input an array, and produces as1307 output the elements of the array added together. This might
2 unmodified lines
1310 as those for the `+` operator (described above).13111312 If the input is an empty array, `add` returns `null`.13131314 `add(generator)` operates on the given generator rather than1315 the input.13161317 examples:1318 - program: add1319 input: '["a","b","c"]'1320 output: ['"abc"']
2 unmodified lines
1323 output: ['6']1324 - program: add1325 input: '[]'1326 output: ["null"]1327 - program: add(.[].a)1328 input: '[{"a":3}, {"a":5}, {"b":6}]'1329 output: ['8']13301331 - title: "`any`, `any(condition)`, `any(generator; condition)`"1332 body: |1333
2473 unmodified lines
modified:src/builtin.jq+2 added-1 removed
6 unmodified lines
7def unique: group_by(.) | map(.[0]);8def unique_by(f): group_by(f) | map(.[0]);9def max_by(f): _max_by_impl(map([f]));10def min_by(f): _min_by_impl(map([f]));11def add: reduce .[] as $x (null; . + $x);11def add(f): reduce f as $x (null; . + $x);12def add: add(.[]);13def del(f): delpaths([path(f)]);14def abs: if . < 0 then - . else . end;15def _assign(paths; $value): reduce path(paths) as $p (.; setpath($p; $value));16def _modify(paths; update):
265 unmodified lines
modified:tests/jq.test+13 added
640 unmodified lines
641map_values(.+1)642[0,1,2]643[1,2,3]644645[add(null), add(range(range(10))), add(empty), add(10,range(10))]646null647[null,120,null,55]648649# Real-world use case for add(empty)650.sum = add(.arr[])651{"arr":[]}652{"arr":[],"sum":null}653654add({(.[]):1}) | keys655["a","a","b","a","d","b","d","a","d"]656["a","b","d"]657658#659# User-defined functions660# Oh god.661#
1530 unmodified lines
modified:tests/man.test+4 added
356 unmodified lines
357add358[]359null360361add(.[].a)362[{"a":3}, {"a":5}, {"b":6}]3638364365any366[true, false]367true368
603 unmodified lines
modified: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 lines
1339.fi1340.1341.IP "" 01342.1343.SS "add"1343.SS "add, add(generator)"1344The 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.P1347If 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 lines
13631364jq \'add\'1365 []1366=> null13671368jq \'add(\.[]\.a)\'1369 [{"a":3}, {"a":5}, {"b":6}]1370=> 81371.1372.fi1373.1374.IP "" 0
2868 unmodified lines