Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement std.minArray #1074

Merged
merged 7 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions doc/_stdlib_gen/stdlib-content.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,16 @@ local html = import 'html.libsonnet';
|||,
]),
},
{
name: 'minArray',
params: ['arr'],
deepgoel17 marked this conversation as resolved.
Show resolved Hide resolved
availableSince: 'upcoming',
description: html.paragraphs([
|||
Return min of all element in <code>arr</code>.
deepgoel17 marked this conversation as resolved.
Show resolved Hide resolved
|||,
]),
},
],
},
{
Expand Down
10 changes: 10 additions & 0 deletions stdlib/std.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -1703,6 +1703,16 @@ limitations under the License.

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

minArray(arr)::
deepgoel17 marked this conversation as resolved.
Show resolved Hide resolved
assert std.length(arr) > 0 : "Expected array with elements. Got emtpy array";
local minVal = arr[0];
local minFn(a,b) =
if std.__compare(a, 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