Skip to content

Commit

Permalink
feat: implement std.minArray (#1074)
Browse files Browse the repository at this point in the history
* Add std.minArray in standard library
  • Loading branch information
deepgoel17 authored May 3, 2023
1 parent e3a330f commit 8ea27ab
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions doc/_stdlib_gen/stdlib-content.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,14 @@ local html = import 'html.libsonnet';
|||,
]),
},
{
name: 'minArray',
params: ['arr', 'keyF', 'onEmpty'],
availableSince: 'upcoming',
description: html.paragraphs([
|||
Return the min of all element in <code>arr</code>.
},
{
name: 'contains',
params: ['arr', 'elem'],
Expand Down
12 changes: 12 additions & 0 deletions stdlib/std.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -1703,6 +1703,18 @@ limitations under the License.

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

minArray(arr, keyF=id, onEmpty=error 'Expected at least one element in array. Got none')::
if std.length(arr) == 0 then
onEmpty
else
local minVal = arr[0];
local minFn(a, b) =
if std.__compare(keyF(a), keyF(b)) > 0 then
b
else
a;
std.foldl(minFn, arr, minVal),

xor(x, y):: x != y,

xnor(x, y):: x == y,
Expand Down
3 changes: 3 additions & 0 deletions test_suite/stdlib.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,9 @@ std.assertEqual(std.all([]), true) &&

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

std.assertEqual(std.minArray([1, 2, 3]), 1) &&
std.assertEqual(std.minArray(['1', '2', '3']), '1') &&

std.assertEqual(std.xor(true, false), true) &&
std.assertEqual(std.xor(true, true), false) &&

Expand Down

0 comments on commit 8ea27ab

Please sign in to comment.