From a556594982dc477a31d72452346e365757998ec5 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Sat, 26 Nov 2022 20:38:06 +0000 Subject: [PATCH] Merge pull request #91 from CertainLach/number-intrinsics --- --- a/crates/jrsonnet-stdlib/src/lib.rs +++ b/crates/jrsonnet-stdlib/src/lib.rs @@ -77,6 +77,10 @@ ("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), ("modulo", builtin_modulo::INST), ("floor", builtin_floor::INST), ("ceil", builtin_ceil::INST), --- a/crates/jrsonnet-stdlib/src/math.rs +++ b/crates/jrsonnet-stdlib/src/math.rs @@ -1,6 +1,26 @@ use jrsonnet_evaluator::{error::Result, function::builtin, typed::PositiveF64}; #[builtin] +pub fn builtin_abs(x: f64) -> Result { + Ok(x.abs()) +} + +#[builtin] +pub fn builtin_sign(x: f64) -> Result { + Ok(if x == 0. { 0. } else { x.signum() }) +} + +#[builtin] +pub fn builtin_max(x: f64, y: f64) -> Result { + Ok(x.max(y)) +} + +#[builtin] +pub fn builtin_min(x: f64, y: f64) -> Result { + Ok(x.min(y)) +} + +#[builtin] pub fn builtin_modulo(a: f64, b: f64) -> Result { Ok(a % b) } --- 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 -- gitstuff