Skip to content

Commit 7edd5d3

Browse files
authored
feat: implement std.maxArray (#696)
1 parent 4bb6e38 commit 7edd5d3

File tree

5 files changed

+41
-1
lines changed

5 files changed

+41
-1
lines changed

builtins.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,6 +1953,42 @@ func builtinMinArray(i *interpreter, arguments []value) (value, error) {
19531953
return minVal, nil
19541954
}
19551955

1956+
func builtinMaxArray(i *interpreter, arguments []value) (value, error) {
1957+
arrv := arguments[0]
1958+
keyFv := arguments[1]
1959+
1960+
arr, err := i.getArray(arrv)
1961+
if err != nil {
1962+
return nil, err
1963+
}
1964+
keyF, err := i.getFunction(keyFv)
1965+
if err != nil {
1966+
return nil, err
1967+
}
1968+
num := arr.length()
1969+
if num == 0 {
1970+
return nil, i.Error("Expected at least one element in array. Got none")
1971+
}
1972+
maxVal, err := keyF.call(i, args(arr.elements[0]))
1973+
if err != nil {
1974+
return nil, err
1975+
}
1976+
for index := 1; index < num; index++ {
1977+
current, err := keyF.call(i, args(arr.elements[index]))
1978+
if err != nil {
1979+
return nil, err
1980+
}
1981+
cmp, err := valueCmp(i, maxVal, current)
1982+
if err != nil {
1983+
return nil, err
1984+
}
1985+
if cmp < 0 {
1986+
maxVal = current
1987+
}
1988+
}
1989+
return maxVal, nil
1990+
}
1991+
19561992
func builtinNative(i *interpreter, name value) (value, error) {
19571993
str, err := i.getString(name)
19581994
if err != nil {
@@ -2385,6 +2421,7 @@ var funcBuiltins = buildBuiltinMap([]builtin{
23852421
&unaryBuiltin{name: "decodeUTF8", function: builtinDecodeUTF8, params: ast.Identifiers{"arr"}},
23862422
&generalBuiltin{name: "sort", function: builtinSort, params: []generalBuiltinParameter{{name: "arr"}, {name: "keyF", defaultValue: functionID}}},
23872423
&generalBuiltin{name: "minArray", function: builtinMinArray, params: []generalBuiltinParameter{{name: "arr"}, {name: "keyF", defaultValue: functionID}}},
2424+
&generalBuiltin{name: "maxArray", function: builtinMaxArray, params: []generalBuiltinParameter{{name: "arr"}, {name: "keyF", defaultValue: functionID}}},
23882425
&unaryBuiltin{name: "native", function: builtinNative, params: ast.Identifiers{"x"}},
23892426
&unaryBuiltin{name: "sum", function: builtinSum, params: ast.Identifiers{"arr"}},
23902427
&binaryBuiltin{name: "contains", function: builtinContains, params: ast.Identifiers{"arr", "elem"}},

linter/internal/types/stdlib.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ func prepareStdlib(g *typeGraph) {
141141
"sort": g.newFuncType(anyArrayType, []ast.Parameter{required("arr"), optional("keyF")}),
142142
"uniq": g.newFuncType(anyArrayType, []ast.Parameter{required("arr"), optional("keyF")}),
143143
"sum": g.newSimpleFuncType(numberType, "arr"),
144-
"minArray": g.newSimpleFuncType(anyType, "arr"),
144+
"minArray": g.newFuncType(anyArrayType, []ast.Parameter{required("arr"), optional("keyF")}),
145+
"maxArray": g.newFuncType(anyArrayType, []ast.Parameter{required("arr"), optional("keyF")}),
145146
"contains": g.newSimpleFuncType(boolType, "arr", "elem"),
146147
"remove": g.newSimpleFuncType(anyArrayType, "arr", "elem"),
147148
"removeAt": g.newSimpleFuncType(anyArrayType, "arr", "i"),

testdata/builtinMaxArray.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1

testdata/builtinMaxArray.jsonnet

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
std.maxArray([1,-1,0])

testdata/builtinMaxArray.linter.golden

Whitespace-only changes.

0 commit comments

Comments
 (0)