Skip to content

Commit

Permalink
feat: add std.is_even, std.is_odd, std.is_integer and std.is_decimal
Browse files Browse the repository at this point in the history
Upstream issue: google/go-jsonnet#702
  • Loading branch information
pawelbeza committed Jul 11, 2023
1 parent 0d33992 commit c0efb56
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions crates/jrsonnet-stdlib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ pub fn stdlib_uncached(settings: Rc<RefCell<Settings>>) -> ObjValue {
("mantissa", builtin_mantissa::INST),
("exponent", builtin_exponent::INST),
("round", builtin_round::INST),
("isEven", builtin_is_even::INST),
("isOdd", builtin_is_odd::INST),
("isInteger", builtin_is_integer::INST),
("isDecimal", builtin_is_decimal::INST),
// Operator
("mod", builtin_mod::INST),
("primitiveEquals", builtin_primitive_equals::INST),
Expand Down
20 changes: 20 additions & 0 deletions crates/jrsonnet-stdlib/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,23 @@ pub fn builtin_exponent(x: f64) -> i16 {
pub fn builtin_round(x: f64) -> f64 {
x.round()
}

#[builtin]
pub fn builtin_is_even(x: f64) -> bool {
builtin_round(x) % 2.0 == 0.0
}

#[builtin]
pub fn builtin_is_odd(x: f64) -> bool {
builtin_round(x) % 2.0 == 1.0
}

#[builtin]
pub fn builtin_is_integer(x: f64) -> bool {
builtin_round(x) == x
}

#[builtin]
pub fn builtin_is_decimal(x: f64) -> bool {
builtin_round(x) != x
}

0 comments on commit c0efb56

Please sign in to comment.