Skip to content

Commit 2a7260d

Browse files
feat: Add more math functions (#702)
Co-authored-by: Dave Cunningham <[email protected]>
1 parent 572c054 commit 2a7260d

26 files changed

+50
-0
lines changed

builtins.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,16 @@ func liftNumeric(f func(float64) float64) func(*interpreter, value) (value, erro
10991099
}
11001100
}
11011101

1102+
func liftNumericToBoolean(f func(float64) bool) func(*interpreter, value) (value, error) {
1103+
return func(i *interpreter, x value) (value, error) {
1104+
n, err := i.getNumber(x)
1105+
if err != nil {
1106+
return nil, err
1107+
}
1108+
return makeValueBoolean(f(n.value)), nil
1109+
}
1110+
}
1111+
11021112
var builtinSqrt = liftNumeric(math.Sqrt)
11031113
var builtinCeil = liftNumeric(math.Ceil)
11041114
var builtinFloor = liftNumeric(math.Floor)
@@ -1125,6 +1135,22 @@ var builtinExponent = liftNumeric(func(f float64) float64 {
11251135
return float64(exponent)
11261136
})
11271137
var builtinRound = liftNumeric(math.Round)
1138+
var builtinIsEven = liftNumericToBoolean(func(f float64) bool {
1139+
i, _ := math.Modf(f) // Get the integral part of the float
1140+
return math.Mod(i, 2) == 0
1141+
})
1142+
var builtinIsOdd = liftNumericToBoolean(func(f float64) bool {
1143+
i, _ := math.Modf(f) // Get the integral part of the float
1144+
return math.Mod(i, 2) != 0
1145+
})
1146+
var builtinIsInteger = liftNumericToBoolean(func(f float64) bool {
1147+
_, frac := math.Modf(f) // Get the fraction part of the float
1148+
return frac == 0
1149+
})
1150+
var builtinIsDecimal = liftNumericToBoolean(func(f float64) bool {
1151+
_, frac := math.Modf(f) // Get the fraction part of the float
1152+
return frac != 0
1153+
})
11281154

11291155
func liftBitwise(f func(int64, int64) int64, positiveRightArg bool) func(*interpreter, value, value) (value, error) {
11301156
return func(i *interpreter, xv, yv value) (value, error) {
@@ -2467,6 +2493,10 @@ var funcBuiltins = buildBuiltinMap([]builtin{
24672493
&unaryBuiltin{name: "mantissa", function: builtinMantissa, params: ast.Identifiers{"x"}},
24682494
&unaryBuiltin{name: "exponent", function: builtinExponent, params: ast.Identifiers{"x"}},
24692495
&unaryBuiltin{name: "round", function: builtinRound, params: ast.Identifiers{"x"}},
2496+
&unaryBuiltin{name: "isEven", function: builtinIsEven, params: ast.Identifiers{"x"}},
2497+
&unaryBuiltin{name: "isOdd", function: builtinIsOdd, params: ast.Identifiers{"x"}},
2498+
&unaryBuiltin{name: "isInteger", function: builtinIsInteger, params: ast.Identifiers{"x"}},
2499+
&unaryBuiltin{name: "isDecimal", function: builtinIsDecimal, params: ast.Identifiers{"x"}},
24702500
&binaryBuiltin{name: "pow", function: builtinPow, params: ast.Identifiers{"x", "n"}},
24712501
&binaryBuiltin{name: "modulo", function: builtinModulo, params: ast.Identifiers{"x", "y"}},
24722502
&unaryBuiltin{name: "md5", function: builtinMd5, params: ast.Identifiers{"s"}},

linter/internal/types/stdlib.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ func prepareStdlib(g *typeGraph) {
4545
"isNumber": g.newSimpleFuncType(boolType, "v"),
4646
"isObject": g.newSimpleFuncType(boolType, "v"),
4747
"isString": g.newSimpleFuncType(boolType, "v"),
48+
"isEven": g.newSimpleFuncType(boolType, "x"),
49+
"isOdd": g.newSimpleFuncType(boolType, "x"),
50+
"isInteger": g.newSimpleFuncType(boolType, "x"),
51+
"isDecimal": g.newSimpleFuncType(boolType, "x"),
4852

4953
// Mathematical utilities
5054
"abs": g.newSimpleFuncType(numberType, "n"),

testdata/builtinIsDecimal.golden

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

testdata/builtinIsDecimal.jsonnet

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

testdata/builtinIsDecimal.linter.golden

Whitespace-only changes.

testdata/builtinIsDecimal2.golden

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

testdata/builtinIsDecimal2.jsonnet

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

testdata/builtinIsDecimal2.linter.golden

Whitespace-only changes.

testdata/builtinIsEven.golden

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

testdata/builtinIsEven.jsonnet

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

0 commit comments

Comments
 (0)