Functional Weave
Code in Python

math.clamp

Clamp an integer to an inclusive range, refusing a range whose bounds are the wrong way round.

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

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

What it does

Returns `value` if it lies between `min` and `max` inclusive, otherwise the nearer bound.

A range with `min` greater than `max` is an error rather than a guess. The common one-liner `max(min, min(value, max))` quietly returns `min` for every input when the bounds are swapped, which hides the bug that swapped them.

For example

  • clamp(5, 0, 10) → 5 a value inside the range is returned unchanged
  • clamp(-3, 0, 10) → 0 below the range gives the lower bound
  • clamp(42, 0, 10) → 10 above the range gives the upper bound

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.

def clamp(value: int, min: int, max: int) -> int
valueint
minintlowest value allowed, inclusive
maxinthighest value allowed, inclusive; must not be below min
returnsint

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

from fune.math.clamp import clamp  # math.clamp@^1
impl/python.py · 16 lines · open · raw
def clamp(value: int, min: int, max: int) -> int:  # noqa: A002 - the manifest names them min and max
    """Clamp ``value`` to the inclusive range ``min..max``.

    Swapped bounds raise: ``max(lo, min(value, hi))`` would answer ``lo`` for
    everything and hide the bug that swapped them.
    """
    for v in (value, min, max):
        if isinstance(v, bool) or not isinstance(v, int):
            raise TypeError("clamp operates on integers only")
    if min > max:
        raise ValueError("min must not be greater than max (min %d, max %d)" % (min, max))
    if value < min:
        return min
    if value > max:
        return max
    return value

Install

fune build

With that line in your source, in a Python project (language python in fune.project), fune build resolves it and nothing else, 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 math.clamp
Download for Python math.clamp-1.0.0-python.fune · 3,849 bytes sha256 257242f4d2aa9023baa8826584fa9e4f4383d88ae04023dbd7dd7ee59c889c27

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

The whole function, every language, is one file too: math.clamp-1.0.0.fune, 5,509 bytes, sha256 2123f0c62d89da1b2ece2862eaf1514f0cbf2690d429dee046b20b51c896f2b4. 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 math.clamp

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

# fune: after math.clamp

replace — it requires no other capability, so there is no dependency to replace.

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 math.clamp --steps.

# fune: step math.clamp 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
a value inside the range is returned unchanged 5, 0, 10 → 5
below the range gives the lower bound -3, 0, 10 → 0
above the range gives the upper bound 42, 0, 10 → 10
exactly the lower bound is inside 0, 0, 10 → 0
exactly the upper bound is inside 10, 0, 10 → 10
a single-point range pins every value 99, 7, 7 → 7
a wholly negative range -50, -20, -10 → -20
a range across zero 3, -5, 5 → 3
a discount capped at 100 percent in basis points 12,500, 0, 10,000 → 10,000
the largest safe integers 9,007,199,254,740,991, -9,007,199,254,740,991, 9,007,199,254,740,990 → 9,007,199,254,740,990
Show the other 2 tests
CaseArgumentsExpected
swapped bounds are an error, not always the lower bound 5, 10, 0 → error: min must not be greater than max
a fractional value is an error 2.5, 0, 10 → error: integers only

More from the author

Integers only, so it is exact for amounts in minor units, basis points and counts. For money of one currency use `money.compare`, which checks the currencies as well.

Files

PathBytes
README.md499
impl/python.py657
impl/rust.rs965
impl/typescript.ts612
vectors.json1,180