add skip/2 as the counterpart to limit/2 (#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.
This commit is contained in:
1 parent
860af44fee
commit
1bebd23aad
5 files changed
+70
-13
No files matched your search
+9
-5
@@ -149,10 +149,14 @@ def until(cond; next):
|
||||
def _until:
|
||||
if cond then . else (next|_until) end;
|
||||
_until;
|
||||
def limit($n; exp):
|
||||
if $n > 0 then label $out | foreach exp as $item ($n; .-1; $item, if . <= 0 then break $out else empty end)
|
||||
elif $n == 0 then empty
|
||||
else exp end;
|
||||
def limit($n; expr):
|
||||
if $n > 0 then label $out | foreach expr as $item ($n; . - 1; $item, if . <= 0 then break $out else empty end)
|
||||
elif $n == 0 then empty
|
||||
else error("limit doesn't support negative count") end;
|
||||
def skip($n; expr):
|
||||
if $n > 0 then foreach expr as $item ($n; . - 1; if . < 0 then $item else empty end)
|
||||
elif $n == 0 then expr
|
||||
else error("skip doesn't support negative count") end;
|
||||
# range/3, with a `by` expression argument
|
||||
def range($init; $upto; $by):
|
||||
if $by > 0 then $init|while(. < $upto; . + $by)
|
||||
@@ -169,7 +173,7 @@ def any: any(.[]; .);
|
||||
def last(g): reduce g as $item (null; $item);
|
||||
def nth($n; g):
|
||||
if $n < 0 then error("nth doesn't support negative indices")
|
||||
else label $out | foreach g as $item ($n + 1; . - 1; if . <= 0 then $item, break $out else empty end) end;
|
||||
else first(skip($n; g)) end;
|
||||
def first: .[0];
|
||||
def last: .[-1];
|
||||
def nth($n): .[$n];
|
||||
|
||||
Reference in new issue
Block a user