git.delta.rocks / jrsonnet / refs/commits / ff649630ca7e

difftreelog

feat implement builtins for trivial numeric functions

Petr Portnov2022-11-26parent: #5f0f8de.patch.diff
in: master

3 files changed

modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -77,6 +77,11 @@
 		("member", builtin_member::INST),
 		("count", builtin_count::INST),
 		// Math
+		("abs", builtin_abs::INST),
+		("sign", builtin_sign::INST),
+		("max", builtin_max::INST),
+		("min", builtin_min::INST),
+		("clamp", builtin_clamp::INST),
 		("modulo", builtin_modulo::INST),
 		("floor", builtin_floor::INST),
 		("ceil", builtin_ceil::INST),
modifiedcrates/jrsonnet-stdlib/src/math.rsdiffbeforeafterboth
1use jrsonnet_evaluator::{error::Result, function::builtin, typed::PositiveF64};1use jrsonnet_evaluator::{error::Result, function::builtin, typed::PositiveF64};
2
3#[builtin]
4pub fn builtin_abs(x: f64) -> Result<f64> {
5 Ok(x.abs())
6}
7
8#[builtin]
9pub fn builtin_sign(x: f64) -> Result<f64> {
10 Ok(if x == 0. { 0. } else { x.signum() })
11}
12
13#[builtin]
14pub fn builtin_max(x: f64, y: f64) -> Result<f64> {
15 Ok(x.max(y))
16}
17
18#[builtin]
19pub fn builtin_min(x: f64, y: f64) -> Result<f64> {
20 Ok(x.min(y))
21}
22
23#[builtin]
24pub fn builtin_clamp(x: f64, min_val: f64, max_val: f64) -> Result<f64> {
25 debug_assert!(x.is_finite(), "jsonnet number are always finite");
26 debug_assert!(min_val.is_finite(), "jsonnet number are always finite");
27 debug_assert!(max_val.is_finite(), "jsonnet number are always finite");
28
29 // `f64::clamp` should noe be used here since it requires extra checks to guarantee NaN-safety
30 Ok(if x < min_val {
31 min_val
32 } else if x > max_val {
33 max_val
34 } else {
35 x
36 })
37}
238
3#[builtin]39#[builtin]
4pub fn builtin_modulo(a: f64, b: f64) -> Result<f64> {40pub fn builtin_modulo(a: f64, b: f64) -> Result<f64> {
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -77,38 +77,6 @@
     else
       error 'Assertion failed. ' + a + ' != ' + b,
 
-  abs(n)::
-    if !std.isNumber(n) then
-      error 'std.abs expected number, got ' + std.type(n)
-    else
-      if n > 0 then n else -n,
-
-  sign(n)::
-    if !std.isNumber(n) then
-      error 'std.sign expected number, got ' + std.type(n)
-    else
-      if n > 0 then
-        1
-      else if n < 0 then
-        -1
-      else 0,
-
-  max(a, b)::
-    if !std.isNumber(a) then
-      error 'std.max first param expected number, got ' + std.type(a)
-    else if !std.isNumber(b) then
-      error 'std.max second param expected number, got ' + std.type(b)
-    else
-      if a > b then a else b,
-
-  min(a, b)::
-    if !std.isNumber(a) then
-      error 'std.min first param expected number, got ' + std.type(a)
-    else if !std.isNumber(b) then
-      error 'std.min second param expected number, got ' + std.type(b)
-    else
-      if a < b then a else b,
-
   clamp(x, minVal, maxVal)::
     if x < minVal then minVal
     else if x > maxVal then maxVal