Skip to content

Commit

Permalink
Make limit/2 more efficient
Browse files Browse the repository at this point in the history
Contributed by @pkoppstein.
  • Loading branch information
nicowilliams committed Mar 6, 2018
1 parent e113c67 commit 7fd9e86
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions src/builtin.jq
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,9 @@ def until(cond; next):
if cond then . else (next|_until) end;
_until;
def limit($n; exp):
if $n < 0 then exp else
label $out |
foreach exp as $item (
[$n, null];
if .[0] < 1 then break $out
else [.[0] -1, $item] end;
if .[0] == 0 then .[1], break $out else .[1] end) end;
if $n < 0 then exp
else label $out | foreach exp as $item ($n; .-1; $item, if . <= 0 then break $out else empty end)
end;
def isempty(g): 0 == ((label $go | g | (1, break $go)) // 0);
def first(g): label $out | g | ., break $out;
def last(g): reduce g as $item (null; $item);
Expand Down

0 comments on commit 7fd9e86

Please sign in to comment.