Skip to content

Commit 1a95727

Browse files
authored
Any all optimization (#974)
* std.any() optimization and std.all() optimization
1 parent 57da10d commit 1a95727

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

stdlib/std.jsonnet

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,23 +1607,34 @@ limitations under the License.
16071607
std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),
16081608

16091609
all(arr)::
1610-
if !std.isArray(arr) then
1611-
error 'all() parameter should be an array, got ' + std.type(arr)
1612-
else
1613-
local and = function(x, y)
1614-
if !std.isBoolean(y) then
1615-
error std.format('element "%s" of type %s is not a boolean', y, std.type(y))
1616-
else x && y;
1617-
std.foldl(and, arr, true),
1610+
assert std.isArray(arr) : 'all() parameter should be an array, got ' + std.type(arr);
1611+
local arrLen = std.length(arr);
1612+
local aux(idx) =
1613+
if idx >= arrLen then
1614+
true
1615+
else
1616+
local e = arr[idx];
1617+
assert std.isBoolean(e) : std.format('element "%s" of type %s is not a boolean', e, std.type(e));
1618+
if !e then
1619+
false
1620+
else
1621+
aux(idx + 1) tailstrict;
1622+
aux(0),
1623+
16181624
any(arr)::
1619-
if !std.isArray(arr) then
1620-
error 'any() parameter should be an array, got ' + std.type(arr)
1621-
else
1622-
local or = function(x, y)
1623-
if !std.isBoolean(y) then
1624-
error std.format('element "%s" of type %s is not a boolean', y, std.type(y))
1625-
else x || y;
1626-
std.foldl(or, arr, false),
1625+
assert std.isArray(arr) : 'any() parameter should be an array, got ' + std.type(arr);
1626+
local arrLen = std.length(arr);
1627+
local aux(idx) =
1628+
if idx >= arrLen then
1629+
false
1630+
else
1631+
local e = arr[idx];
1632+
assert std.isBoolean(e) : std.format('element "%s" of type %s is not a boolean', e, std.type(e));
1633+
if e then
1634+
true
1635+
else
1636+
aux(idx + 1) tailstrict;
1637+
aux(0),
16271638

16281639
// Three way comparison.
16291640
// TODO(sbarzowski): consider exposing and documenting it properly

0 commit comments

Comments
 (0)