Functional Weave
Code in Python

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

def compare_money(a: Money, b: Money) -> int
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
from fune.money.compare import compare_money  # money.compare@^2
impl/python/compare_money.py · 11 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.

from .money_amount import Money, assert_same_currency  ← from money.amount ^1.0.0 · built alongside by fune


def compare_money(a: Money, b: Money) -> int:
    """-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.
    """
    assert_same_currency(a, b)
    return -1 if a.minor < b.minor else 1 if a.minor > b.minor else 0

min_money throws on bad input 9 tests

def 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
from fune.money.compare import min_money  # money.compare@^2
impl/python/min_money.py · 7 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.

from .money_amount import Money  ← from money.amount ^1.0.0 · built alongside by fune
from .money_compare_compare_money import compare_money  ← compareMoney, another function of this group · built into the same file, even by a slim install


def min_money(a: Money, b: Money) -> Money:
    """The smaller amount; the first when they are equal."""
    return b if compare_money(b, a) < 0 else a

max_money throws on bad input 8 tests

def 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
from fune.money.compare import max_money  # money.compare@^2
impl/python/max_money.py · 7 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.

from .money_amount import Money  ← from money.amount ^1.0.0 · built alongside by fune
from .money_compare_compare_money import compare_money  ← compareMoney, another function of this group · built into the same file, even by a slim install


def max_money(a: Money, b: Money) -> Money:
    """The larger amount; the first when they are equal."""
    return b if compare_money(b, a) > 0 else a

clamp_money throws on bad input 11 tests

def 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
from fune.money.compare import clamp_money  # money.compare@^2
impl/python/clamp_money.py · 14 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.

from .math_clamp import clamp  ← from math.clamp ^1.0.0 · built alongside by fune
from .money_amount import Money, assert_same_currency, money  ← from money.amount ^1.0.0 · built alongside by fune


def clamp_money(amount: Money, min: Money, max: Money) -> Money:  # noqa: A002 - the manifest names them min and max
    """``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.
    """
    assert_same_currency(amount, min)
    assert_same_currency(amount, max)
    return money(clamp(amount.minor, min.minor, max.minor), amount.currency)

Install

fune build

With that line in your source, in a Python project (language python in fune.project), fune build resolves it and its 2 dependencies, pins them in fune.lock, downloads only the Python 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. 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 Python money.compare-2.0.0-python.fune · 16,648 bytes sha256 d6cca37235853dab248fab2fde5ff83458260880de07d096b1d5461832e8613e

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

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