Functional Weave
Code in TypeScript

finance.depreciation-straight-line

Straight-line depreciation schedule whose charges sum exactly to cost less residual value.

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

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

What it does

The depreciable amount (cost less residual value) is split with money.allocate rather than divided by the number of years, so the charges add up to it exactly and the final closing value is the residual value to the penny. Dividing and rounding each year instead leaves 1000.00 over three years as 333.33 three times, a penny short, and an asset that never finishes writing down.

The odd minor units land in the earliest years, because that is money.allocate's largest-remainder rule and this capability does not invent a second, conflicting one. For 1000.00 over three years the charges are 333.34, 333.33, 333.33. An accounting policy that wants the difference in the final year should adjust the last charge itself, where the choice is visible.

For example

  • straightLineDepreciation(£1,200.00, £0.00, 3) → ×3 1200.00 over three years divides evenly: 400.00 a year, down to nothing
  • straightLineDepreciation(£1,000.00, £0.00, 3) → ×3 1000.00 over three years does not divide evenly and the odd penny is kept, not dropped
  • straightLineDepreciation(£1,000.00, £100.00, 3) → ×3 a residual value is written down to, not through

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 straightLineDepreciation(cost: Money, residualValue: Money, years: number): readonly DepreciationPeriod[]
costMoneycapitalised cost of the asset
residualValueMoneyexpected value at the end of its life; 0 if written down to nothing
yearsintuseful life in whole years, at least 1
returnsDepreciationPeriod[]

The type it declares, generated into your project

/** One year of the schedule. */
export interface DepreciationPeriod {
  /** 1-based: year 1 is the first full year of the asset's life */
  readonly year: number;
  readonly openingValue: Money;
  readonly charge: Money;
  readonly closingValue: Money;
}

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

import { straightLineDepreciation } from "#fune/finance.depreciation-straight-line@^1";
impl/typescript.ts · 61 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 { allocate } from "./money_allocate.ts";  ← from money.allocate ^1.0.0 · built alongside by fune
import { subtractMoney } from "./money_add.ts";  ← from money.add ^1.0.0 · built alongside by fune
import type { Money } from "./money_amount.ts";  ← from money.amount ^1.0.0 · built alongside by fune
import { type DepreciationPeriod } from "./finance_depreciation_straight_line_types.ts";

/**
 * A straight-line depreciation schedule, year by year.
 *
 * The depreciable amount is split with money.allocate instead of being divided
 * by the number of years. Dividing leaves 1000.00 over three years as 333.33
 * three times - a penny short - and an asset that never quite finishes writing
 * down to its residual value. allocate hands out every minor unit, so the last
 * closing value equals the residual value exactly, by construction rather than
 * by a fudge at the end.
 *
 * The odd units go to the earliest years, following allocate's largest-
 * remainder rule rather than a second rule invented here.
 */
export function straightLineDepreciation(
  cost: Money,
  residualValue: Money,
  years: number,
): readonly DepreciationPeriod[] {
  if (!Number.isInteger(years) || years < 1) {
    throw new RangeError(`years must be at least 1, received ${years}`);
  }
  if (cost.minor < 0) {
    throw new RangeError("cost must not be negative");
  }
  if (residualValue.minor < 0) {
    throw new RangeError("residual value must not be negative");
  }

  // subtractMoney also rejects a cost and a residual value in different
  // currencies, which would otherwise produce a plausible-looking schedule.
  const depreciable = subtractMoney(cost, residualValue);
  if (depreciable.minor < 0) {
    throw new RangeError("residual value must not exceed cost");
  }

  const charges = allocate(depreciable, new Array<number>(years).fill(1));

  const schedule: DepreciationPeriod[] = [];
  let openingValue = cost;
  for (let index = 0; index < years; index += 1) {
    const charge = charges[index];
    const closingValue = subtractMoney(openingValue, charge);
    schedule.push({ year: index + 1, openingValue, charge, closingValue });
    openingValue = closingValue;
  }
  return schedule;
}

/** The written-down value after `elapsedYears` complete years. */
export function writtenDownValue(cost: Money, residualValue: Money, years: number, elapsedYears: number): Money {
  const schedule = straightLineDepreciation(cost, residualValue, years);
  if (!Number.isInteger(elapsedYears) || elapsedYears < 0 || elapsedYears > years) {
    throw new RangeError(`elapsedYears must be between 0 and ${years}, received ${elapsedYears}`);
  }
  return elapsedYears === 0 ? cost : schedule[elapsedYears - 1].closingValue;
}

Install

fune build

With that line in your source, in a TypeScript project (language typescript in fune.project), fune build resolves it and its 3 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 finance.depreciation-straight-line
Download for TypeScript finance.depreciation-straight-line-1.0.0-typescript.fune · 14,739 bytes sha256 68088aa81bc87f488c414f7d1102311c690729905ff11fd79e64b678831db2fb

The manifest, vectors and README with only the TypeScript implementation. Install it without the registry with fune add ./finance.depreciation-straight-line-1.0.0-typescript.fune, or fetch it from a terminal with fune pull finance.depreciation-straight-line@1.0.0:typescript.

The whole function, every language, is one file too: finance.depreciation-straight-line-1.0.0.fune, 21,560 bytes, sha256 0695a6a3ea4324d63b1e7b07067487ebe7fb608afa73b34475838341835c6495. 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 finance.depreciation-straight-line

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

// fune: after finance.depreciation-straight-line

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 finance.depreciation-straight-line
// fune: replace money.allocate in finance.depreciation-straight-line
// fune: replace money.amount in finance.depreciation-straight-line

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 finance.depreciation-straight-line --steps.

// fune: step finance.depreciation-straight-line 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
1200.00 over three years divides evenly: 400.00 a year, down to nothing £1,200.00, £0.00, 3 → ×3
1000.00 over three years does not divide evenly and the odd penny is kept, not dropped £1,000.00, £0.00, 3 → ×3
a residual value is written down to, not through £1,000.00, £100.00, 3 → ×3
100.00 over seven years: four years carry the odd penny and the asset still lands on zero £100.00, £0.00, 7 → ×7
a one year life charges everything in year one £500.00, £100.00, 1 → ×1
an asset already at its residual value has nothing to depreciate £750.00, £750.00, 4 → ×4
a worthless asset gives a schedule of zeroes rather than an error £0.00, £0.00, 2 → ×2
an awkward residual value still closes exactly on it £1,000.00, £333.33, 3 → ×3
yen has no minor unit, so the odd unit is a whole yen ¥1,000, ¥0, 3 → ×3
a zero year life is an error, not an instant write-off £1,000.00, £0.00, 0 → error: years must be at least 1
Show the other 5 tests
CaseArgumentsExpected
a fractional life is an error £1,000.00, £0.00, 2.5 → error: years must be at least 1
a residual value above cost is an error, not negative depreciation £1,000.00, £1,500.00, 3 → error: residual value must not exceed cost
a negative residual value is an error: disposal costs are not depreciation £1,000.00, -£1.00, 3 → error: residual value must not be negative
a negative cost is an error: a credit is not an asset -£1,000.00, £0.00, 3 → error: cost must not be negative
costing in one currency and scrapping in another is an error £1,000.00, €100.00, 3 → error: currency mismatch

More from the author

The schedule is returned whole - opening value, charge and closing value for every year - because the closing value is the number that has to agree with the balance sheet, and recomputing it from the charges in caller code is where it stops agreeing.

A full year is charged in each of the `years` years: this is the simple convention, not a mid-month or mid-year one. Pro-rating the first and last period is finance.proration's job.

Files

PathBytes
README.md1,222
impl/python.py2,677
impl/rust.rs3,909
impl/typescript.ts2,565
vectors.json7,544