Functional Weave
Code in Python

logistics.route-distance

Total great-circle distance of an ordered list of stops, leg by leg, optionally back to the start, to the millimetre.

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

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

What it does

The length of a route that visits the given stops in the given order: the great-circle distance of each leg from `geo.distance`, and their total. With `returnToStart` a closing leg from the last stop back to the first is added, for a van that comes back to the depot.

It does not choose the order (that is route optimisation, a much bigger problem) and it measures as the crow flies. Road distance is typically 20-40% longer; callers that quote it multiply by their own circuity factor, and callers that need the real figure need a routing engine.

For example

  • route_distance(stops ×2, false) → total metres 343,556.535, leg metres 343,556.535 London to Paris is one leg
  • route_distance(stops ×2, true) → total metres 687,113.07, leg metres 343,556.535, 343,556.535 round trip adds the leg home
  • route_distance(stops ×3, false) → total metres 222,390.16, leg metres 111,195.08, 111,195.08 along the equator then north, a degree each

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 route_distance(stops: Sequence[GeoPoint], return_to_start: bool) -> RouteDistance
stopsGeoPoint[]in the order they are visited; at least one
return_to_startbooladd a closing leg from the last stop back to the first, for a round trip
returnsRouteDistance

The type it declares, generated into your project

@dataclass(frozen=True)
class RouteDistance:
    """The whole route and each leg, in metres to the millimetre."""

    #: the sum of the legs, summed exactly in millimetres
    total_metres: float
    #: one per leg: stops[0] to stops[1], stops[1] to stops[2], ...
    leg_metres: List[float]

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

from fune.logistics.route_distance import route_distance  # logistics.route-distance@^1
impl/python.py · 29 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.

import math
from typing import List, Sequence

from .geo_distance import distance  ← from geo.distance ^1.0.0 · built alongside by fune
from .geo_point_in_polygon import GeoPoint  ← from geo.point-in-polygon ^1.0.0 · built alongside by fune
from .logistics_route_distance_types import RouteDistance


def route_distance(stops: Sequence[GeoPoint], return_to_start: bool) -> RouteDistance:
    """Great-circle length of a route through the stops in order, leg by leg,
    optionally closing back to the first stop."""
    if len(stops) == 0:
        raise ValueError("stops must not be empty")
    leg_metres: List[float] = []
    # Each leg is a whole number of millimetres; adding them as integers and
    # dividing once avoids floating-point drift (and is the same double everywhere).
    total_millimetres = 0

    def add_leg(a: GeoPoint, b: GeoPoint) -> None:
        nonlocal total_millimetres
        metres = distance(a.lat, a.lng, b.lat, b.lng)
        leg_metres.append(metres)
        total_millimetres += math.floor(metres * 1000 + 0.5)

    for i in range(1, len(stops)):
        add_leg(stops[i - 1], stops[i])
    if return_to_start and len(stops) > 1:
        add_leg(stops[-1], stops[0])
    return RouteDistance(total_metres=total_millimetres / 1000, leg_metres=leg_metres)

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 logistics.route-distance
Download for Python logistics.route-distance-1.0.0-python.fune · 7,973 bytes sha256 743e45e0360d5680e4117d25f6fa3600f8239cc1db7282f2cf41bf2ba5a719a4

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

The whole function, every language, is one file too: logistics.route-distance-1.0.0.fune, 10,910 bytes, sha256 466edb4c820220ce3afbcc8bdacd67502202c89f2f192576ce8b885c7f53a9a8. 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 logistics.route-distance

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

# fune: after logistics.route-distance

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 geo.distance in logistics.route-distance
# fune: replace geo.point-in-polygon in logistics.route-distance

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 logistics.route-distance --steps.

# fune: step logistics.route-distance 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
London to Paris is one leg stops ×2, false → total metres 343,556.535, leg metres 343,556.535
round trip adds the leg home stops ×2, true → total metres 687,113.07, leg metres 343,556.535, 343,556.535
along the equator then north, a degree each stops ×3, false → total metres 222,390.16, leg metres 111,195.08, 111,195.08
three legs summed in floating point would be 8659345.290000001 stops ×4, false → total metres 8,659,345.29, leg metres 2,886,448.43, 2,886,448.43, 2,886,448.43
five legs summed in floating point would be 1717782.6749999998 stops ×6, false → total metres 1,717,782.675, leg metres 343,556.535, 343,556.535, 343,556.535, 343,556.535, 343,556.535
across the antimeridian is one degree, not 359 stops ×2, false → total metres 111,195.08, leg metres 111,195.08
a repeated stop is a leg of zero stops ×3, false → total metres 343,556.535, leg metres 0, 343,556.535
one stop has no legs stops ×1, false → total metres 0, leg metres
one stop has nowhere to return from stops ×1, true → total metres 0, leg metres
no stops is an error, not zero , false → error: stops must not be empty
Show the other 2 tests
CaseArgumentsExpected
a latitude out of range is an error stops ×3, false → error: latitude must be between -90 and 90 degrees
a longitude out of range is an error stops ×2, false → error: longitude must be between -180 and 180 degrees

More from the author

## Exact totals

Each leg is `geo.distance`'s answer, a whole number of millimetres expressed in metres. Adding those as floating-point numbers drifts: three legs of 2,886,448.43 m add up to 8,659,345.290000001 m. So each leg is turned back into integer millimetres, the millimetres are added as integers, and the total is divided by 1000 once, which gives the same double in every language (8,659,345.29). The manifest says `floats exact` and the vectors hold the three implementations to it.

## Edge cases

- One stop is a route of no legs: total 0, `legMetres` empty, with or without `returnToStart` (there is nowhere to return from). - Two identical stops in a row are a leg of 0. - An empty list is an error, not 0: it usually means the stops were never loaded. - Coordinates are checked by `geo.distance`: out of range is an error, not wrapped, because it usually means latitude and longitude were swapped.

## Types

A stop is `GeoPoint` from `geo.point-in-polygon`, the registry's one latitude/longitude record, so a polygon's vertices and a route's stops are the same type. That capability is required for its type only.

Files

PathBytes
README.md1,715
impl/python.py1,176
impl/rust.rs1,698
impl/typescript.ts1,127
vectors.json2,679