Skip to content

Commit 1bebd23

Browse files
authored
add skip/2 as the counterpart to limit/2 (jqlang#3181)
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.
1 parent 860af44 commit 1bebd23

File tree

5 files changed

+70
-13
lines changed

5 files changed

+70
-13
lines changed

docs/content/manual/dev/manual.yml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3037,16 +3037,26 @@ sections:
30373037
input: '[1,2,3]'
30383038
output: ['false']
30393039

3040-
- title: "`limit(n; exp)`"
3040+
- title: "`limit(n; expr)`"
30413041
body: |
30423042
3043-
The `limit` function extracts up to `n` outputs from `exp`.
3043+
The `limit` function extracts up to `n` outputs from `expr`.
30443044
30453045
examples:
3046-
- program: '[limit(3;.[])]'
3046+
- program: '[limit(3; .[])]'
30473047
input: '[0,1,2,3,4,5,6,7,8,9]'
30483048
output: ['[0,1,2]']
30493049

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+
30503060
- title: "`first(expr)`, `last(expr)`, `nth(n; expr)`"
30513061
body: |
30523062

jq.1.prebuilt

Lines changed: 19 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/builtin.jq

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,14 @@ def until(cond; next):
149149
def _until:
150150
if cond then . else (next|_until) end;
151151
_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 empty
155-
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 empty
155+
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 expr
159+
else error("skip doesn't support negative count") end;
156160
# range/3, with a `by` expression argument
157161
def range($init; $upto; $by):
158162
if $by > 0 then $init|while(. < $upto; . + $by)
@@ -169,7 +173,7 @@ def any: any(.[]; .);
169173
def last(g): reduce g as $item (null; $item);
170174
def nth($n; g):
171175
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;
173177
def first: .[0];
174178
def last: .[-1];
175179
def nth($n): .[$n];

tests/jq.test

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,30 @@ null
334334
"badness"
335335
[1]
336336

337+
try limit(-1; error) catch .
338+
null
339+
"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+
null
355+
"skip doesn't support negative count"
356+
357+
nth(1; 0,1,error("foo"))
358+
null
359+
1
360+
337361
[first(range(.)), last(range(.))]
338362
10
339363
[0,9]

tests/man.test

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)