Skip to content
Myr

Make limit/2 more efficient

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Contributed by @pkoppstein.

authored by Nicolas Williams · committed by Nicolas Williams · parent e113c67 · Browse files · 1 file+3 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+3 added−7 removed

  • other:+3 added−7 removed

1 file+3 added−7 removed

modified:src/builtin.jq+3 added-7 removed
166 unmodified lines
167 def _until:168 if cond then . else (next|_until) end;169 _until;170def limit($n; exp):171 if $n < 0 then exp else172 label $out |173 foreach exp as $item (174 [$n, null];175 if .[0] < 1 then break $out176 else [.[0] -1, $item] end;177 if .[0] == 0 then .[1], break $out else .[1] end) end;171 if $n < 0 then exp172 else label $out | foreach exp as $item ($n; .-1; $item, if . <= 0 then break $out else empty end)173 end;174def isempty(g): 0 == ((label $go | g | (1, break $go)) // 0);175def first(g): label $out | g | ., break $out;176def last(g): reduce g as $item (null; $item);177def nth($n; g): if $n < 0 then error("nth doesn't support negative indices") else last(limit($n + 1; g)) end;
134 unmodified lines