Skip to content

Commit f758f3e

Browse files
committed
add skip/2 as the counterpart to limit/2
1 parent 860af44 commit f758f3e

File tree

5 files changed

+76
-11
lines changed

5 files changed

+76
-11
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: 7 additions & 3 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)
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)
154154
elif $n == 0 then empty
155-
else exp end;
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)

tests/jq.test

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,38 @@ 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(3; .[])]
346+
[]
347+
[]
348+
349+
[skip(0; .[])]
350+
[1,2,3]
351+
[1,2,3]
352+
353+
[skip(2; .[])]
354+
[1,2,3]
355+
[3]
356+
357+
[skip(3; .[])]
358+
[1,2,3]
359+
[]
360+
361+
[skip(4; .[])]
362+
[1,2,3]
363+
[]
364+
365+
try skip(-1; error) catch .
366+
null
367+
"skip doesn't support negative count"
368+
337369
[first(range(.)), last(range(.))]
338370
10
339371
[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)