Functional Weave
Code in Rust

hr.absence-bradford-factor@1.0.0

impl/rust.rs

991 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

/// B = S x S x D over the spells given.
///
/// # Panics
/// Panics if a spell is shorter than one day.
pub fn bradford_factor(spell_days: &[i64]) -> BradfordFactor {
    let mut days = 0;
    for &d in spell_days {
        if d < 1 {
            panic!("each spell must be at least one whole day, received {}", d);
        }
        days += d;
    }
    let spells = spell_days.len() as i64;
    BradfordFactor { spells, days, score: spells * spells * days }
}

pub fn fune_vector(args: &[Value]) -> Value {
    let spells: Vec<i64> = args[0]
        .as_arr()
        .iter()
        .map(|v| match v {
            Value::Float(f) => panic!("each spell must be at least one whole day, received {}", f),
            other => other.as_i64(),
        })
        .collect();
    let b = bradford_factor(&spells);
    Value::obj(vec![
        ("spells", Value::Int(b.spells)),
        ("days", Value::Int(b.days)),
        ("score", Value::Int(b.score)),
    ])
}