Skip to content
Myr

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

5 files+70 added−13 removed

modified:docs/content/manual/dev/manual.yml+13 added-3 removed
3035 unmodified lines
3036 - program: 'isempty(.[])'3037 input: '[1,2,3]'3038 output: ['false']30393040 - title: "`limit(n; exp)`"3040 - title: "`limit(n; expr)`"3041 body: |30423043 The `limit` function extracts up to `n` outputs from `exp`.3043 The `limit` function extracts up to `n` outputs from `expr`.30443045 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]']30493050 - title: "`skip(n; expr)`"3051 body: |30523053 The `skip` function skips the first `n` outputs from `expr`.30543055 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]']30593060 - title: "`first(expr)`, `last(expr)`, `nth(n; expr)`"3061 body: |30623063 The `first(expr)` and `last(expr)` functions extract the first
759 unmodified lines
modified:src/builtin.jq+9 added-5 removed
147 unmodified lines
148def until(cond; next):149 def _until:150 if cond then . else (next|_until) end;151 _until;152def 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;152def 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;156def 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 argument161def range($init; $upto; $by):162 if $by > 0 then $init|while(. < $upto; . + $by)163 elif $by < 0 then $init|while(. > $upto; . + $by)
8 unmodified lines
172def any: any(.[]; .);173def last(g): reduce g as $item (null; $item);174def 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;177def first: .[0];178def last: .[-1];179def nth($n): .[$n];180def combinations:
105 unmodified lines
modified:tests/jq.test+24 added
332 unmodified lines
333[limit(1; 1, error)]334"badness"335[1]336337try limit(-1; error) catch .338null339"limit doesn't support negative count"340341[skip(3; .[])]342[1,2,3,4,5,6,7,8,9]343[4,5,6,7,8,9]344345[skip(0,2,3,4; .[])]346[1,2,3]347[1,2,3,3]348349[skip(3; .[])]350[]351[]352353try skip(-1; error) catch .354null355"skip doesn't support negative count"356357nth(1; 0,1,error("foo"))358null3591360361[first(range(.)), last(range(.))]36210363[0,9]364
1860 unmodified lines
modified:tests/man.test+5 added-1 removed
871 unmodified lines
872isempty(.[])873[1,2,3]874false875876[limit(3;.[])]876[limit(3; .[])]877[0,1,2,3,4,5,6,7,8,9]878[0,1,2]879880[skip(3; .[])]881[0,1,2,3,4,5,6,7,8,9]882[3,4,5,6,7,8,9]883884[first(range(.)), last(range(.)), nth(./2; range(.))]88510886[0,9,5]887
88 unmodified lines
modified: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 lines
3434.fi3435.3436.IP "" 03437.3438.SS "limit(n; exp)"3439The \fBlimit\fR function extracts up to \fBn\fR outputs from \fBexp\fR\.3438.SS "limit(n; expr)"3439The \fBlimit\fR function extracts up to \fBn\fR outputs from \fBexpr\fR\.3440.3441.IP "" 43442.3443.nf34443445jq \'[limit(3;\.[])]\'3445jq \'[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)"3454The \fBskip\fR function skips the first \fBn\fR outputs from \fBexpr\fR\.3455.3456.IP "" 43457.3458.nf34593460jq \'[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)"3469The \fBfirst(expr)\fR and \fBlast(expr)\fR functions extract the first and last values from \fBexpr\fR, respectively\.3470.3471.P
792 unmodified lines