difftreelog
feat add std.avg
in: master
Upstream issue: https://github.com/google/jsonnet/pull/1087
3 files changed
crates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth1#![allow(non_snake_case)]21use jrsonnet_evaluator::{3use jrsonnet_evaluator::{2 error::{ErrorKind::RuntimeError, Result},4 error::{ErrorKind::RuntimeError, Result},3 function::{builtin, FuncVal},5 function::{builtin, FuncVal},4 throw,6 throw,5 typed::{BoundedI32, BoundedUsize, Either2, NativeFn, Typed},7 typed::{BoundedI32, BoundedUsize, Either2, NativeFn, Typed},6 val::{equals, ArrValue, IndexableVal, StrValue},8 val::{equals, ArrValue, IndexableVal, StrValue},7 Either, IStr, Val,9 Either, IStr, Thunk, Val,8};10};1112pub(crate) fn eval_on_empty(on_empty: Option<Thunk<Val>>) -> Result<Val> {13 if let Some(on_empty) = on_empty {14 on_empty.evaluate()15 } else {16 throw!("expected non-empty array")17 }18}91910#[builtin]20#[builtin]11pub fn builtin_make_array(sz: BoundedI32<0, { i32::MAX }>, func: FuncVal) -> Result<ArrValue> {21pub fn builtin_make_array(sz: BoundedI32<0, { i32::MAX }>, func: FuncVal) -> Result<ArrValue> {231 Ok(count)241 Ok(count)232}242}243244#[builtin]245pub fn builtin_avg(arr: Vec<f64>, onEmpty: Option<Thunk<Val>>) -> Result<Val> {246 if arr.is_empty() {247 return eval_on_empty(onEmpty);248 }249 Ok(Val::Num(arr.iter().sum::<f64>() / (arr.len() as f64)))250}233251crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth81 ("all", builtin_all::INST),81 ("all", builtin_all::INST),82 ("member", builtin_member::INST),82 ("member", builtin_member::INST),83 ("count", builtin_count::INST),83 ("count", builtin_count::INST),84 ("avg", builtin_avg::INST),84 // Math85 // Math85 ("abs", builtin_abs::INST),86 ("abs", builtin_abs::INST),86 ("sign", builtin_sign::INST),87 ("sign", builtin_sign::INST),crates/jrsonnet-stdlib/src/sort.rsdiffbeforeafterboth13use jrsonnet_gcmodule::Cc;13use jrsonnet_gcmodule::Cc;14use jrsonnet_parser::BinaryOpType;14use jrsonnet_parser::BinaryOpType;1516use crate::eval_on_empty;151716#[derive(Copy, Clone)]18#[derive(Copy, Clone)]17enum SortKeyType {19enum SortKeyType {207 }209 }208}210}209211210fn eval_on_empty(on_empty: Option<Thunk<Val>>) -> Result<Val> {211 if let Some(on_empty) = on_empty {212 on_empty.evaluate()213 } else {214 throw!("expected non-empty array")215 }216}217212218fn eval_keyf(val: Val, key_f: &Option<FuncVal>) -> Result<Val> {213fn eval_keyf(val: Val, key_f: &Option<FuncVal>) -> Result<Val> {219 if let Some(key_f) = key_f {214 if let Some(key_f) = key_f {