Functional Weave
Code in Python

money.sum@1.0.1

impl/rust.rs

590 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
use super::money_add::add_money;  ← from money.add ^1.0.0 · built alongside by fune
use super::money_amount::{money, money_from_value, money_to_value, Money};  ← from money.amount ^1.0.0 · built alongside by fune

/// # Panics
/// Panics if any amount is in a different currency.
pub fn sum_money(amounts: &[Money], currency: &str) -> Money {
    let mut total = money(0, currency);
    for amount in amounts {
        total = add_money(&total, amount);
    }
    total
}

pub fn fune_vector(args: &[Value]) -> Value {
    let amounts: Vec<Money> = args[0].as_arr().iter().map(money_from_value).collect();
    money_to_value(&sum_money(&amounts, args[1].as_str()))
}