diff options
author | Nicolas Williams <nico@cryptonector.com> | 2018-03-06 13:55:16 -0600 |
---|---|---|
committer | Nicolas Williams <nico@cryptonector.com> | 2018-03-06 16:35:33 -0600 |
commit | 7fd9e86ea694bcfc3cb8472d43c93626febd45cd (patch) | |
tree | 72a4dc16a29b0ba50b98ed9d054f02f3af66b29f | |
parent | e113c67d5116e52e536fae8f2d52516bd2cefa31 (diff) |
Make limit/2 more efficient
Contributed by @pkoppstein.
-rw-r--r-- | src/builtin.jq | 10 |
1 files changed, 3 insertions, 7 deletions
diff --git a/src/builtin.jq b/src/builtin.jq index c445f1a2..e65a8997 100644 --- a/src/builtin.jq +++ b/src/builtin.jq @@ -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); |