Functional Weave
Code in Rust

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

  • straight_line_depreciation(£1,200.00, £0.00, 3) → ×3 1200.00 over three years divides evenly: 400.00 a year, down to nothing
  • straight_line_depreciation(£1,000.00, £0.00, 3) → ×3 1000.00 over three years does not divide evenly and the odd penny is kept, not dropped
  • straight_line_depreciation(£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.

pub fn straight_line_depreciation(cost: &Money, residual_value: &Money, years: i64) -> Vec<DepreciationPeriod>
costMoneycapitalised cost of the asset
residual_valueMoneyexpected 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.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DepreciationPeriod {
    /// 1-based: year 1 is the first full year of the asset's life
    pub year: i64,
    pub opening_value: Money,
    pub charge: Money,
    pub closing_value: Money,
}

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

fune!(finance.depreciation-straight-line@^1);  // then call straight_line_depreciation(…)
impl/rust.rs · 112 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.

use super::funejson::Value;  ← the fune runtime: the JSON value the test vectors use; fune build keeps it only where a signature takes one
use super::money_add::subtract_money;  ← from money.add ^1.0.0 · built alongside by fune
use super::money_allocate::allocate;  ← from money.allocate ^1.0.0 · built alongside by fune
use super::money_amount::{money_from_value, money_to_value, Money};  ← from money.amount ^1.0.0 · built alongside by fune

/// 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.
///
/// # Panics
/// Panics if `years` is below 1, either amount is negative, the residual value
/// exceeds the cost, or the two amounts are in different currencies.
pub fn straight_line_depreciation(
    cost: &Money,
    residual_value: &Money,
    years: i64,
) -> Vec<DepreciationPeriod> {
    if years < 1 {
        panic!("years must be at least 1, received {}", years);
    }
    if cost.minor < 0 {
        panic!("cost must not be negative");
    }
    if residual_value.minor < 0 {
        panic!("residual value must not be negative");
    }

    // subtract_money also rejects a cost and a residual value in different
    // currencies, which would otherwise produce a plausible-looking schedule.
    let depreciable = subtract_money(cost, residual_value);
    if depreciable.minor < 0 {
        panic!("residual value must not exceed cost");
    }

    let ratios = vec![1i64; years as usize];
    let charges = allocate(&depreciable, &ratios);

    let mut schedule: Vec<DepreciationPeriod> = Vec::with_capacity(years as usize);
    let mut opening_value = cost.clone();
    for (index, charge) in charges.iter().enumerate() {
        let closing_value = subtract_money(&opening_value, charge);
        schedule.push(DepreciationPeriod {
            year: index as i64 + 1,
            opening_value: opening_value.clone(),
            charge: charge.clone(),
            closing_value: closing_value.clone(),
        });
        opening_value = closing_value;
    }
    schedule
}

/// The written-down value after `elapsed_years` complete years.
///
/// # Panics
/// Panics if `elapsed_years` is outside `0..=years`.
pub fn written_down_value(
    cost: &Money,
    residual_value: &Money,
    years: i64,
    elapsed_years: i64,
) -> Money {
    let schedule = straight_line_depreciation(cost, residual_value, years);
    if !(0..=years).contains(&elapsed_years) {
        panic!(
            "elapsed_years must be between 0 and {}, received {}",
            years, elapsed_years
        );
    }
    if elapsed_years == 0 {
        cost.clone()
    } else {
        schedule[elapsed_years as usize - 1].closing_value.clone()
    }
}

pub fn depreciation_period_to_value(period: &DepreciationPeriod) -> Value {
    Value::obj(vec![
        ("year", Value::Int(period.year)),
        ("openingValue", money_to_value(&period.opening_value)),
        ("charge", money_to_value(&period.charge)),
        ("closingValue", money_to_value(&period.closing_value)),
    ])
}

pub fn fune_vector(args: &[Value]) -> Value {
    // Refuse what the typed signature cannot hold, with the wording TypeScript
    // and Python use, rather than let the conversion below quietly change it.
    if let Value::Float(f) = args[2] {
        if f.fract() != 0.0 {
            panic!("years must be at least 1, received {}", f);
        }
    }
    Value::Arr(
        straight_line_depreciation(
            &money_from_value(&args[0]),
            &money_from_value(&args[1]),
            args[2].as_i64(),
        )
        .iter()
        .map(depreciation_period_to_value)
        .collect(),
    )
}

Install

fune build

With that line in your source, in a Rust project (language rust in fune.project), fune build resolves it and its 3 dependencies, pins them in fune.lock, downloads only the Rust 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. A crate’s build.rs runs it before every compile. Or pin a range in fune.project and build in one step:

fune add finance.depreciation-straight-line
Download for Rust finance.depreciation-straight-line-1.0.0-rust.fune · 16,128 bytes sha256 4ba6b7b8a9d5a5a670915179a0516b059477f362054572e086beb4818ba71e0e

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

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