add skip/2 as the counterpart to limit/2 (#3181)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
I also changed the behavior of limit/2 with negative count to emit an error, like nth/2 does. Also, I redefined nth/2 using skip/2 to minimize the impact of growth of builtin filters.
authored by itchyny · committed by GitHub · parent 860af44 · Browse files · 5 files·+70 added−13 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+70 added−13 removed
- tests:+29 added−1 removed
- other:+41 added−12 removed
Provenance
- Commit
1bebd23- Tree
f734d6f- Author
itchyny <itchyny@cybozu.co.jp>- Committer
GitHub <noreply@github.com>- Parent
860af44
5 files changed+70 added−13 removed
modified:docs/content/manual/dev/manual.yml+13 added-3 removed
3035 unmodified lines3036 - program: 'isempty(.[])'3037 input: '[1,2,3]'3038 output: ['false']3039 3040- - title: "`limit(n; exp)`"3040+ - title: "`limit(n; expr)`"3041 body: |3042 3043- The `limit` function extracts up to `n` outputs from `exp`.3043+ The `limit` function extracts up to `n` outputs from `expr`.3044 3045 examples:3046- - program: '[limit(3;.[])]'3046+ - program: '[limit(3; .[])]'3047 input: '[0,1,2,3,4,5,6,7,8,9]'3048 output: ['[0,1,2]']3049 3050+ - title: "`skip(n; expr)`"3051+ body: |3052+3053+ The `skip` function skips the first `n` outputs from `expr`.3054+3055+ examples:3056+ - program: '[skip(3; .[])]'3057+ input: '[0,1,2,3,4,5,6,7,8,9]'3058+ output: ['[3,4,5,6,7,8,9]']3059+3060 - title: "`first(expr)`, `last(expr)`, `nth(n; expr)`"3061 body: |3062 3063 The `first(expr)` and `last(expr)` functions extract the first759 unmodified linesmodified:src/builtin.jq+9 added-5 removed
147 unmodified lines148 def until(cond; next):149 def _until:150 if cond then . else (next|_until) end;151 _until;152-def limit($n; exp):153- if $n > 0 then label $out | foreach exp as $item ($n; .-1; $item, if . <= 0 then break $out else empty end)154- elif $n == 0 then empty155- else exp end;152+def limit($n; expr):153+ if $n > 0 then label $out | foreach expr as $item ($n; . - 1; $item, if . <= 0 then break $out else empty end)154+ elif $n == 0 then empty155+ else error("limit doesn't support negative count") end;156+def skip($n; expr):157+ if $n > 0 then foreach expr as $item ($n; . - 1; if . < 0 then $item else empty end)158+ elif $n == 0 then expr159+ else error("skip doesn't support negative count") end;160 # range/3, with a `by` expression argument161 def range($init; $upto; $by):162 if $by > 0 then $init|while(. < $upto; . + $by)163 elif $by < 0 then $init|while(. > $upto; . + $by)8 unmodified lines172 def any: any(.[]; .);173 def last(g): reduce g as $item (null; $item);174 def nth($n; g):175 if $n < 0 then error("nth doesn't support negative indices")172- else label $out | foreach g as $item ($n + 1; . - 1; if . <= 0 then $item, break $out else empty end) end;176+ else first(skip($n; g)) end;177 def first: .[0];178 def last: .[-1];179 def nth($n): .[$n];180 def combinations:105 unmodified linesmodified:tests/jq.test+24 added
332 unmodified lines333 [limit(1; 1, error)]334 "badness"335 [1]336 337+try limit(-1; error) catch .338+null339+"limit doesn't support negative count"340+341+[skip(3; .[])]342+[1,2,3,4,5,6,7,8,9]343+[4,5,6,7,8,9]344+345+[skip(0,2,3,4; .[])]346+[1,2,3]347+[1,2,3,3]348+349+[skip(3; .[])]350+[]351+[]352+353+try skip(-1; error) catch .354+null355+"skip doesn't support negative count"356+357+nth(1; 0,1,error("foo"))358+null359+1360+361 [first(range(.)), last(range(.))]362 10363 [0,9]364 1860 unmodified linesmodified:tests/man.test+5 added-1 removed
871 unmodified lines872 isempty(.[])873 [1,2,3]874 false875 876-[limit(3;.[])]876+[limit(3; .[])]877 [0,1,2,3,4,5,6,7,8,9]878 [0,1,2]879 880+[skip(3; .[])]881+[0,1,2,3,4,5,6,7,8,9]882+[3,4,5,6,7,8,9]883+884 [first(range(.)), last(range(.)), nth(./2; range(.))]885 10886 [0,9,5]887 88 unmodified linesmodified:jq.1.prebuilt+19 added-4 removed
1 .2-.TH "JQ" "1" "August 2024" "" ""2+.TH "JQ" "1" "September 2024" "" ""3 .4 .SH "NAME"5 \fBjq\fR \- Command\-line JSON processor6 .3427 unmodified lines3434 .fi3435 .3436 .IP "" 03437 .3438-.SS "limit(n; exp)"3439-The \fBlimit\fR function extracts up to \fBn\fR outputs from \fBexp\fR\.3438+.SS "limit(n; expr)"3439+The \fBlimit\fR function extracts up to \fBn\fR outputs from \fBexpr\fR\.3440 .3441 .IP "" 43442 .3443 .nf3444 3445-jq \'[limit(3;\.[])]\'3445+jq \'[limit(3; \.[])]\'3446 [0,1,2,3,4,5,6,7,8,9]3447 => [0,1,2]3448 .3449 .fi3450 .3451 .IP "" 03452 .3453+.SS "skip(n; expr)"3454+The \fBskip\fR function skips the first \fBn\fR outputs from \fBexpr\fR\.3455+.3456+.IP "" 43457+.3458+.nf3459+3460+jq \'[skip(3; \.[])]\'3461+ [0,1,2,3,4,5,6,7,8,9]3462+=> [3,4,5,6,7,8,9]3463+.3464+.fi3465+.3466+.IP "" 03467+.3468 .SS "first(expr), last(expr), nth(n; expr)"3469 The \fBfirst(expr)\fR and \fBlast(expr)\fR functions extract the first and last values from \fBexpr\fR, respectively\.3470 .3471 .P792 unmodified lines