Functional Weave
Code in Python

charts.extent@1.0.0

impl/rust.rs

998 bytes · the Rust implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

use super::funejson::Value;  ← the fune runtime: the JSON value the test vectors use; fune build keeps it only where a signature takes one

/// [min, max] of a series, skipping gaps (`None`); `None` when nothing is left.
///
/// # Panics
/// Panics on a value that is not finite.
pub fn extent(values: &[Option<f64>]) -> Option<Vec<f64>> {
    let mut range: Option<(f64, f64)> = None;
    for v in values.iter().flatten() {
        if !v.is_finite() {
            panic!("values must be finite numbers or null, received {}", v);
        }
        range = Some(match range {
            None => (*v, *v),
            Some((lo, hi)) => (if *v < lo { *v } else { lo }, if *v > hi { *v } else { hi }),
        });
    }
    range.map(|(lo, hi)| vec![lo, hi])
}

pub fn fune_vector(args: &[Value]) -> Value {
    let values: Vec<Option<f64>> = args[0]
        .as_arr()
        .iter()
        .map(|v| if v.is_null() { None } else { Some(v.as_f64()) })
        .collect();
    match extent(&values) {
        Some(r) => Value::Arr(r.into_iter().map(Value::Float).collect()),
        None => Value::Null,
    }
}