Functional Weave
Code in Rust

money.allocate

Split an amount across ratios without losing or inventing a single minor unit.

1.0.0 (not the latest) · published 2026-10-03 by charlie · Anterra

Pinned by 7 tests, run in TypeScript, Python and Rust · fewer than the registry now requires. 1.0.1 adds them.

What it does

Splitting 10.00 three ways gives 3.34, 3.33, 3.33 - never 3.33 three times with a penny quietly lost, and never 3.34 three times with a penny invented.

The leftover is distributed by largest remainder, ties broken by position, so the same input always produces the same split in every language.

For example

  • allocate(£10.00, 1, 1, 1) → £3.34, £3.33, £3.33 ten pounds three ways keeps the penny
  • allocate(£5.00, 3, 7) → £1.50, £3.50 exact split needs no remainder
  • allocate(£100.00, 1, 1, 1, 1, 1, 1) → £16.67, £16.67, £16.67, £16.67, £16.66, £16.66 weighted split distributes by largest remainder

The function

The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.

pub fn allocate(amount: &Money, ratios: &[i64]) -> Vec<Money>
amountMoney
ratiosint[]relative weights; [1,1,1] is an even three-way split
returnsMoney[]

Your code names it in one line, in the file that uses it

fune!(money.allocate@^1);  // then call allocate(…)
impl/rust.rs · 61 lines · open · 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_amount::{money, money_from_value, money_to_value, Money};  ← from money.amount ^1.0.0 · built alongside by fune

/// Split `amount` across `ratios` so the parts add back up to the whole.
///
/// Splitting 10.00 three ways gives 3.34, 3.33, 3.33 - never 3.33 three times
/// with a penny quietly lost. The leftover goes to the largest remainders,
/// ties broken by position, so the split is stable and reproducible.
///
/// # Panics
/// Panics if `ratios` is empty or sums to zero.
pub fn allocate(amount: &Money, ratios: &[i64]) -> Vec<Money> {
    if ratios.is_empty() {
        panic!("allocate needs at least one ratio");
    }
    let total: i64 = ratios.iter().sum();
    if total == 0 {
        panic!("ratios must not sum to zero");
    }

    let mut bases: Vec<i64> = Vec::with_capacity(ratios.len());
    let mut remainders: Vec<i64> = Vec::with_capacity(ratios.len());
    let total128 = total as i128;
    for ratio in ratios {
        // Euclidean division keeps the remainder non-negative, which is the
        // invariant the largest-remainder pass below relies on, and matches
        // floor division in the TypeScript and Python implementations.
        let numerator = (amount.minor as i128) * (*ratio as i128);
        let base = numerator.div_euclid(total128);
        bases.push(base as i64);
        remainders.push(numerator.rem_euclid(total128) as i64);
    }

    let mut leftover = amount.minor - bases.iter().sum::<i64>();

    let mut order: Vec<usize> = (0..ratios.len()).collect();
    order.sort_by(|a, b| remainders[*b].cmp(&remainders[*a]).then(a.cmp(b)));

    for index in order {
        if leftover <= 0 {
            break;
        }
        bases[index] += 1;
        leftover -= 1;
    }

    bases
        .iter()
        .map(|base| money(*base, &amount.currency))
        .collect()
}

pub fn fune_vector(args: &[Value]) -> Value {
    let ratios: Vec<i64> = args[1].as_arr().iter().map(|v| v.as_i64()).collect();
    Value::Arr(
        allocate(&money_from_value(&args[0]), &ratios)
            .iter()
            .map(money_to_value)
            .collect(),
    )
}

Install

fune build

With that line in your source, in a Rust project (language rust in fune.project), fune build resolves it and its 1 dependency, pins them in fune.lock, downloads only the Rust package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. A crate’s build.rs runs it before every compile. Or pin a range in fune.project and build in one step:

fune add money.allocate
Download for Rust money.allocate-1.0.0-rust.fune · 5,427 bytes sha256 aa8f1ea8970252e9dfab1967c15075c3ecad72cf48fb88a05ec3ac9c8ba24ec8

The manifest, vectors and README with only the Rust implementation. Install it without the registry with fune add ./money.allocate-1.0.0-rust.fune, or fetch it from a terminal with fune pull money.allocate@1.0.0:rust.

The whole function, every language, is one file too: money.allocate-1.0.0.fune, 8,253 bytes, sha256 94885a2d1582976c8f966a662a880b69d40bf0c991ab03766ae6d08d096b2655. It installs into a project of any language.

Customise it in your app

The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.

before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.

// fune: before money.allocate

after — your function gets the result and the arguments, and returns the final result.

// fune: after money.allocate

replace — inside this capability’s code only, calls to a dependency go to your function, with the same signature. Other capabilities that use it are unaffected; write in * to replace it everywhere.

// fune: replace money.amount in money.allocate

step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show money.allocate --steps.

// fune: step money.allocate after <n|label>

Tests

A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.

This version has fewer tests than the registry now requires. It was published before every function had to have 8. 1.0.1 meets it, and a project on ^1.0.0 installs that or newer.

  • allocate has 7 tests; every function needs at least 8. Add 1 more to vectors.json: the ordinary case, the boundaries (zero, negative, the largest values), the rounding edge and every error it documents
CaseArgumentsExpected
ten pounds three ways keeps the penny £10.00, 1, 1, 1 → £3.34, £3.33, £3.33
exact split needs no remainder £5.00, 3, 7 → £1.50, £3.50
weighted split distributes by largest remainder £100.00, 1, 1, 1, 1, 1, 1 → £16.67, £16.67, £16.67, £16.67, £16.66, £16.66
credit notes split without inventing money -£10.00, 1, 1, 1 → -£3.33, -£3.33, -£3.34
single share takes everything £9.99, 1 → £9.99
zero ratios are an error £1.00, 0, 0 → error: ratios must not sum to zero
empty ratios are an error £1.00, → error: at least one ratio

Files

PathBytes
README.md314
impl/python.py1,279
impl/rust.rs2,108
impl/typescript.ts1,414
vectors.json1,471