builtin.jq: naive abs/0
Before adoption: this commit came in with the history the project adopted, so no landing records it.
manual.yml explains that the def is naive, and mentions fabs, etc.
authored by pkoppstein · committed by Nico Williams · parent 3a1ba0c · Browse files · 5 files·+53 added−0 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+53 added−0 removed
- tests:+16 added−0 removed
- other:+37 added−0 removed
Provenance
- Commit
7b72537- Tree
fd279b6- Author
pkoppstein <pkoppstein@gmail.com>- Committer
Nico Williams <nico@cryptonector.com>- Parent
3a1ba0c
5 files changed+53 added−0 removed
modified:docs/content/manual/manual.yml+15 added
848 unmodified lines849 - program: '.[] | (1 / .)?'850 input: '[1,0,-1]'851 output: ['1', '-1']852 853+ - title: "`abs`"854+ body: |855 856+ The builtin function `abs` is defined naively as: `if . < 0 then - . else . end`.857+858+ For numeric input, this is the absolute value. See the859+ section on the identity filter for the implications of this860+ definition for numeric input.861+862+ To compute the absolute value of a number as a floating point number, you may wish use `fabs`.863+864+ examples:865+ - program: 'map(abs)'866+ input: '[-10, -1.1, -1e-1, 1000000000000000002, -1000000000000000002]'867+ output: ['[10,1.1,1e-1,10000000000000002,1e+18]']868+869 - title: "`length`"870 body: |871 872 The builtin function `length` gets the length of various2767 unmodified linesmodified:src/builtin.jq+1 added
8 unmodified lines9 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);12 def del(f): delpaths([path(f)]);13+def abs: if . < 0 then - . else . end;14 def _assign(paths; $value): reduce path(paths) as $p (.; setpath($p; $value));15 def _modify(paths; update):16 reduce path(paths) as $p ([., []];17 . as $dot260 unmodified linesmodified:tests/jq.test+12 added
1806 unmodified lines1807 . |= try . catch .1808 11809 11810 1811+# abs, fabs, length1812+abs1813+"abc"1814+"abc"1815 1816+map(abs)1817+[-10, -1.1, -1e-1, 1000000000000000002]1818+[10,1.1,0.1,1000000000000000002]1819+1820+map(fabs == length) | unique[0]1821+[-10, -1.1, -1e-1, 1000000000000000002, -1000000000000000002]1822+true1823+1824 # Using a keyword as variable/label name1825 1826 123 as $label | $label1827 null112 unmodified linesmodified:tests/man.test+4 added
186 unmodified lines187 [1,0,-1]188 1189 -1190 191+map(abs)192+[-10, -1.1, -1e-1, 1000000000000000002, -1000000000000000002]193+[10,1.1,1e-1,10000000000000002,1e+18]194+195 .[] | length196 [[1,2], "string", {"a":2}, null, -5]197 2198 6749 unmodified linesmodified:jq.1.prebuilt+21 added
833 unmodified lines834 .fi835 .836 .IP "" 0837 .838+.SS "abs"839+The builtin function \fBabs\fR is defined naively as: \fBif \. < 0 then \- \. else \. end\fR\.840+.841+.P842+For numeric input, this is the absolute value\. See the section on the identity filter for the implications of this definition for numeric input\.843+.844+.P845+To compute the absolute value of a number as a floating point number, you may wish use \fBfabs\fR\.846+.847+.IP "" 4848+.849+.nf850+851+jq \'map(abs)\'852+ [\-10, \-1\.1, \-1e\-1, 1000000000000000002, \-1000000000000000002]853+=> [10,1\.1,1e\-1,10000000000000002,1e+18]854+.855+.fi856+.857+.IP "" 0858+.859 .SS "length"860 The builtin function \fBlength\fR gets the length of various different types of value:861 .862 .IP "\(bu" 43228 unmodified lines