Functional Weave
Code in Rust

money.compare

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

2.0.0 · published 2026-10-03 by charlie · Anterra

Pinned by 39 tests, run in TypeScript, Python and Rust.compareMoney 11 · minMoney 9 · maxMoney 8 · clampMoney 11

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`.

The functions

A group: 4 functions that work together, each in its own file, each pinned by its own tests in TypeScript, Python and Rust. A project can install only the ones it calls.

  1. compare_money (a: Money, b: Money) -> int
  2. min_money (a: Money, b: Money) -> Money
  3. max_money (a: Money, b: Money) -> Money
  4. clamp_money (amount: Money, min: Money, max: Money) -> Money

Once installed, your code imports each one from the group's module.

compare_money throws on bad input 11 tests

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

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
fune!(money.compare@^2);  // then call compare_money(…)
impl/rust/compare_money.rs · 25 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::{assert_same_currency, 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,
    }
}

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

min_money throws on bad input 9 tests

pub fn min_money(a: &Money, b: &Money) -> Money
aMoney
bMoney
returnsMoneythe smaller amount; a when they are equal

For example

  • min_money(£5.00, £3.00) → £3.00 the smaller of two amounts
  • min_money(£3.00, £5.00) → £3.00 order does not matter
  • min_money(£19.99, £19.99) → £19.99 equal amounts give that amount
fune!(money.compare@^2);  // then call min_money(…)
impl/rust/min_money.rs · 18 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_from_value, money_to_value, Money};  ← from money.amount ^1.0.0 · built alongside by fune
use super::money_compare_compare_money::compare_money;  ← compareMoney, another function of this group · built into the same file, even by a slim install

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

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

max_money throws on bad input 8 tests

pub fn max_money(a: &Money, b: &Money) -> Money
aMoney
bMoney
returnsMoneythe larger amount; a when they are equal

For example

  • max_money(£5.00, £3.00) → £5.00 the larger of two amounts
  • max_money(£3.00, £5.00) → £5.00 order does not matter
  • max_money(£19.99, £19.99) → £19.99 equal amounts give that amount
fune!(money.compare@^2);  // then call max_money(…)
impl/rust/max_money.rs · 18 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_from_value, money_to_value, Money};  ← from money.amount ^1.0.0 · built alongside by fune
use super::money_compare_compare_money::compare_money;  ← compareMoney, another function of this group · built into the same file, even by a slim install

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

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

clamp_money throws on bad input 11 tests

pub fn clamp_money(amount: &Money, min: &Money, max: &Money) -> Money
amountMoney
minMoneylowest amount allowed, inclusive
maxMoneyhighest amount allowed, inclusive; must not be below min
returnsMoney

For example

  • clamp_money(£7.50, £5.00, £10.00) → £7.50 inside the range it is unchanged
  • clamp_money(£1.00, £5.00, £10.00) → £5.00 below the range it is lifted to min
  • clamp_money(£25.00, £5.00, £10.00) → £10.00 above the range it is capped at max
fune!(money.compare@^2);  // then call clamp_money(…)
impl/rust/clamp_money.rs · 23 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_to_value, Money};  ← from money.amount ^1.0.0 · built alongside by fune

/// `amount` held within `min..=max`, all three in one currency. Bounds the
/// wrong way round are an error (from math.clamp), not an answer: capping a
/// refund at the original charge with the bounds swapped is a bug to surface.
///
/// # 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 {
    money_to_value(&clamp_money(
        &money_from_value(&args[0]),
        &money_from_value(&args[1]),
        &money_from_value(&args[2]),
    ))
}

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

That builds the whole group. To build only what you call, and whatever it uses inside the group:

fune add money.compare --only compareMoney
Download for Rust money.compare-2.0.0-rust.fune · 17,946 bytes sha256 b74cb2910b4f3c914e018ca5499062423bb841e29736c2c32e5e7f484d55d96b

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

The whole function, every language, is one file too: money.compare-2.0.0.fune, 21,448 bytes, sha256 be921740d107bfb3f817355a1724582ab8bf91eb38f3977ac0a346c71dfcc541. 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.compareMoney
// fune: before money.compare.minMoney
// fune: before money.compare.maxMoney
// fune: before money.compare.clampMoney

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

// fune: after money.compare.compareMoney
// fune: after money.compare.minMoney
// fune: after money.compare.maxMoney
// fune: after money.compare.clampMoney

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 a 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.<fn> 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.

compareMoney 11 tests

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

minMoney 9 tests

CaseArgumentsExpected
the smaller of two amounts £5.00, £3.00 → £3.00
order does not matter £3.00, £5.00 → £3.00
equal amounts give that amount £19.99, £19.99 → £19.99
a refund is smaller than a charge -£2.50, £1.00 → -£2.50
the larger debt is the smaller amount -£5.00, -£1.00 → -£5.00
zero against a one penny credit £0.00, -£0.01 → -£0.01
yen compare in whole yen ¥1,500, ¥1,499 → ¥1,499
opposite extremes of the safe range £90,071,992,547,409.91, -£90,071,992,547,409.91 → -£90,071,992,547,409.91
mixed currencies are an error, not an answer £1.00, €2.00 → error: currency mismatch

maxMoney 8 tests

CaseArgumentsExpected
the larger of two amounts £5.00, £3.00 → £5.00
order does not matter £3.00, £5.00 → £5.00
equal amounts give that amount £19.99, £19.99 → £19.99
a charge is larger than a refund -£2.50, £1.00 → £1.00
the smaller debt is the larger amount -£5.00, -£1.00 → -£1.00
zero against a one penny credit £0.00, -£0.01 → £0.00
one minor unit apart at the top of the safe range £90,071,992,547,409.90, £90,071,992,547,409.91 → £90,071,992,547,409.91
mixed currencies are an error, not an answer $1.00, £1.00 → error: currency mismatch

clampMoney 11 tests

CaseArgumentsExpected
inside the range it is unchanged £7.50, £5.00, £10.00 → £7.50
below the range it is lifted to min £1.00, £5.00, £10.00 → £5.00
above the range it is capped at max £25.00, £5.00, £10.00 → £10.00
exactly min is kept £5.00, £5.00, £10.00 → £5.00
exactly max is kept £10.00, £5.00, £10.00 → £10.00
a refund is capped at the original charge, as a negative range -£15.00, -£10.00, £0.00 → -£10.00
zero-width range pins the amount £0.42, £0.00, £0.00 → £0.00
a range entirely of credits -£0.10, -£5.00, -£1.00 → -£1.00
bounds the wrong way round are an error £7.50, £10.00, £5.00 → error: min must not be greater than max
a min in another currency is an error £7.50, €5.00, £10.00 → error: currency mismatch
Show the other 1 test
CaseArgumentsExpected
a max in another currency is an error £7.50, £5.00, $10.00 → error: currency mismatch

More from the author

`minMoney(a, b)` and `maxMoney(a, b)` are built on `compareMoney`, with the same currency check, and return the first argument when the two are equal. `clampMoney(amount, min, max)` holds an amount within an inclusive range, all three in one currency, using `math.clamp`. It refuses a range whose `min` is above its `max` ("min must not be greater than max"): 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.

This is a group of four functions, each in its own file. `minMoney` and `maxMoney` import `compareMoney`, so installing either with `only=` brings it too; `clampMoney` stands alone.

## What changed from 1.0.0

1.0.0 was one function, `compareMoney`, with `minMoney`, `maxMoney` and `clampMoney` exported beside it but unpinned: no signature in the manifest and no vectors, so nothing held the three languages to the same answer for them. 2.0.0 is a group in which all four are published functions with their own signatures and vectors. `compareMoney` keeps every 1.0.0 vector.

No answer changed. It is a new major version because the package's shape changed: it installs as one module per function plus the group module (`money_compare` still re-exports all four), and a project can take only some of it. Dependents stay on `^1.0.0` until they move deliberately; 1.0.0 is unchanged.

Files

PathBytes
README.md1,874
impl/python/clamp_money.py614
impl/python/compare_money.py436
impl/python/max_money.py240
impl/python/min_money.py241
impl/rust/clamp_money.rs887
impl/rust/compare_money.rs812
impl/rust/max_money.rs559
impl/rust/min_money.rs560
impl/typescript/clamp_money.ts576
impl/typescript/compare_money.ts448
impl/typescript/max_money.ts268
impl/typescript/min_money.ts269
vectors.json7,670