Skip to content

Commit 8ea27ab

Browse files
authored
feat: implement std.minArray (#1074)
* Add std.minArray in standard library
1 parent e3a330f commit 8ea27ab

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

doc/_stdlib_gen/stdlib-content.jsonnet

+8
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,14 @@ local html = import 'html.libsonnet';
14211421
|||,
14221422
]),
14231423
},
1424+
{
1425+
name: 'minArray',
1426+
params: ['arr', 'keyF', 'onEmpty'],
1427+
availableSince: 'upcoming',
1428+
description: html.paragraphs([
1429+
|||
1430+
Return the min of all element in <code>arr</code>.
1431+
},
14241432
{
14251433
name: 'contains',
14261434
params: ['arr', 'elem'],

stdlib/std.jsonnet

+12
Original file line numberDiff line numberDiff line change
@@ -1703,6 +1703,18 @@ limitations under the License.
17031703

17041704
sum(arr):: std.foldl(function(a, b) a + b, arr, 0),
17051705

1706+
minArray(arr, keyF=id, onEmpty=error 'Expected at least one element in array. Got none')::
1707+
if std.length(arr) == 0 then
1708+
onEmpty
1709+
else
1710+
local minVal = arr[0];
1711+
local minFn(a, b) =
1712+
if std.__compare(keyF(a), keyF(b)) > 0 then
1713+
b
1714+
else
1715+
a;
1716+
std.foldl(minFn, arr, minVal),
1717+
17061718
xor(x, y):: x != y,
17071719

17081720
xnor(x, y):: x == y,

test_suite/stdlib.jsonnet

+3
Original file line numberDiff line numberDiff line change
@@ -1544,6 +1544,9 @@ std.assertEqual(std.all([]), true) &&
15441544

15451545
std.assertEqual(std.sum([1, 2, 3]), 6) &&
15461546

1547+
std.assertEqual(std.minArray([1, 2, 3]), 1) &&
1548+
std.assertEqual(std.minArray(['1', '2', '3']), '1') &&
1549+
15471550
std.assertEqual(std.xor(true, false), true) &&
15481551
std.assertEqual(std.xor(true, true), false) &&
15491552

0 commit comments

Comments
 (0)