money.sum
Total a list of monetary amounts, with an explicit currency for the empty case.
1.0.0 (not the latest) · published 2026-10-03 by charlie · Anterra
Pinned by 4 tests, run in TypeScript, Python and Rust · fewer than the registry now requires. 1.0.1 adds them.
What it does
The currency argument looks redundant until the list is empty, which is exactly when `reduce` without an initial value throws in production.
For example
sumMoney(£10.00, £25.50, £0.99, GBP)→ £36.49 totals three linessumMoney(, GBP)→ £0.00 empty list is zero in the stated currencysumMoney(£50.00, -£12.00, GBP)→ £38.00 credits reduce the total
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.
export function sumMoney(amounts: readonly Money[], currency: string): Money
| amounts | Money[] | |
| currency | string | the currency of the result, required so an empty list still has one |
| returns | Money |
Your code names it in one line, in the file that uses it
import { sumMoney } from "#fune/money.sum@^1";
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
import { type Money, money } from "./money_amount.ts"; ← from money.amount ^1.0.0 · built alongside by fune
import { addMoney } from "./money_add.ts"; ← from money.add ^1.0.0 · built alongside by fune
export function sumMoney(amounts: readonly Money[], currency: string): Money {
return amounts.reduce<Money>((total, amount) => addMoney(total, amount), money(0, currency));
}Install
fune build
With that line in your source, in a TypeScript project (language typescript in fune.project), fune build resolves it and its 2 dependencies, pins them in fune.lock, downloads only the TypeScript 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.sum
The manifest, vectors and README with only the TypeScript implementation. Install it without the registry with fune add ./money.sum-1.0.0-typescript.fune, or fetch it from a terminal with fune pull money.sum@1.0.0:typescript.
The whole function, every language, is one file too: money.sum-1.0.0.fune, 3,488 bytes, sha256 dc2958a37f07f5a3ae28ad6f788ff55623c7a0cf37daafc8ad3fc01e4005009e. 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.sum
after — your function gets the result and the arguments, and returns the final result.
// fune: after money.sum
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.add in money.sum
// fune: replace money.amount in money.sum
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.sum --steps.
// fune: step money.sum 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.
- sumMoney has 4 tests; every function needs at least 8. Add 4 more to vectors.json: the ordinary case, the boundaries (zero, negative, the largest values), the rounding edge and every error it documents
| Case | Arguments | Expected | |
|---|---|---|---|
| totals three lines | £10.00, £25.50, £0.99, GBP | → | £36.49 |
| empty list is zero in the stated currency | , GBP | → | £0.00 |
| credits reduce the total | £50.00, -£12.00, GBP | → | £38.00 |
| a foreign amount in the list is an error | £1.00, $1.00, GBP | → | error: currency mismatch |
Files
| Path | Bytes |
|---|---|
| README.md | 154 |
| impl/python.py | 284 |
| impl/rust.rs | 590 |
| impl/typescript.ts | 276 |
| vectors.json | 716 |