Functional Weave
Code in Rust

money.compare

Compare two amounts of one currency, refusing to compare across currencies; min, max and clamp too.

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

Pinned by 11 tests, run in TypeScript, Python and Rust.

What it does

`compareMoney(a, b)` is -1, 0 or 1, the shape every sort function takes. It is exactly -1, 0 or 1, not the difference `a - b`, so a caller can switch on it and it cannot overflow.

Comparing GBP 10 with EUR 10 is an error ("currency mismatch"), not an answer. Which of the two is larger depends on an exchange rate, and a comparison that quietly looks only at the numbers will sort a mixed-currency list into nonsense. Convert first with `money.convert`.

For example

  • compare_money(£1.00, £2.00) → -1 less is -1
  • compare_money(£2.00, £1.00) → 1 greater is 1
  • compare_money(£19.99, £19.99) → 0 equal is 0

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 compare_money(a: &Money, b: &Money) -> i64
aMoney
bMoney
returnsint-1 if a is less than b, 0 if equal, 1 if greater

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

fune!(money.compare@^1);  // then call compare_money(…)
impl/rust.rs · 46 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::math_clamp::clamp;  ← from math.clamp ^1.0.0 · built alongside by fune
use super::money_amount::{assert_same_currency, money, money_from_value, Money};  ← from money.amount ^1.0.0 · built alongside by fune

/// -1, 0 or 1, comparing two amounts of the same currency.
///
/// Refuses mixed currencies: which of GBP 10 and EUR 10 is larger depends on a
/// rate, and a comparison that ignores the currency sorts a list into nonsense.
///
/// # Panics
/// Panics if the two amounts are in different currencies.
pub fn compare_money(a: &Money, b: &Money) -> i64 {
    assert_same_currency(a, b);
    match a.minor.cmp(&b.minor) {
        std::cmp::Ordering::Less => -1,
        std::cmp::Ordering::Equal => 0,
        std::cmp::Ordering::Greater => 1,
    }
}

/// The smaller amount; the first when they are equal.
pub fn min_money(a: &Money, b: &Money) -> Money {
    if compare_money(b, a) < 0 { b.clone() } else { a.clone() }
}

/// The larger amount; the first when they are equal.
pub fn max_money(a: &Money, b: &Money) -> Money {
    if compare_money(b, a) > 0 { b.clone() } else { a.clone() }
}

/// `amount` held within `min..=max`, all three in one currency.
///
/// # Panics
/// Panics on mixed currencies or if `min` is above `max`.
pub fn clamp_money(amount: &Money, min: &Money, max: &Money) -> Money {
    assert_same_currency(amount, min);
    assert_same_currency(amount, max);
    money(clamp(amount.minor, min.minor, max.minor), &amount.currency)
}

pub fn fune_vector(args: &[Value]) -> Value {
    Value::Int(compare_money(
        &money_from_value(&args[0]),
        &money_from_value(&args[1]),
    ))
}

Install

fune build

With that line in your source, in a Rust project (language rust in fune.project), fune build resolves it and its 2 dependencies, 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.compare
Download for Rust money.compare-1.0.0-rust.fune · 6,774 bytes sha256 16ebe06b3a840a5fcd9874f66b5d3305c102551b2965562e221f8797a09e348d

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

The whole function, every language, is one file too: money.compare-1.0.0.fune, 9,081 bytes, sha256 a2f90e08a0b24b24f121a1d6df2604bc4d7f49028d1aeeaf71dcd14cd8162520. 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.compare

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

// fune: after money.compare

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 math.clamp in money.compare
// fune: replace money.amount in money.compare

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.compare --steps.

// fune: step money.compare 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.

CaseArgumentsExpected
less is -1 £1.00, £2.00 → -1
greater is 1 £2.00, £1.00 → 1
equal is 0 £19.99, £19.99 → 0
the answer is 1, not the difference of 1998 £19.99, £0.01 → 1
a larger debt is less -£5.00, -£1.00 → -1
zero is more than a credit £0.00, -£0.01 → 1
one minor unit apart £100.00, £100.01 → -1
yen compares in whole yen ¥1,500, ¥1,499 → 1
the largest safe amounts still compare exactly £90,071,992,547,409.91, £90,071,992,547,409.90 → 1
opposite extremes -£90,071,992,547,409.91, £90,071,992,547,409.91 → -1
Show the other 1 test
CaseArgumentsExpected
comparing across currencies is an error, not an answer £10.00, €10.00 → error: currency mismatch

More from the author

The module also exports `minMoney(a, b)`, `maxMoney(a, b)` and `clampMoney(amount, min, max)`, built on `compareMoney` and `math.clamp` with the same currency check. `minMoney` and `maxMoney` return the first argument when the two are equal. `clampMoney` refuses a range whose `min` is above its `max`, as `math.clamp` does: capping a refund at the original charge, or a discount at the order total, with the bounds the wrong way round is a bug to surface, not a value to return.

Files

PathBytes
README.md953
impl/python.py1,088
impl/rust.rs1,557
impl/typescript.ts1,098
vectors.json2,476