333 formulas
A small spreadsheet where fold computes the formulas, so an edit recalculates only what depends on it.
What fold does here
Every edit is stored as changes, with a list of edits in force, so the whole workbook can be scrubbed back. Each formula is compiled into terms. A term's range is spread into rows (this cell feeds that formula), joined with the cell values, and totalled by fold's sum, min and max. Simple arithmetic over the terms is summed by fold too.
The caption under the grid counts how many formulas fold actually re-evaluated after the last change.
Why it shows off fold
A spreadsheet is incremental computation in its most familiar form, which is exactly what fold is for. Change one cell and fold touches only the formulas whose ranges contain it, so an edit costs about the same whether the sheet has a thousand formulas or six thousand. And because every edit can be taken back, undo isn't a stack of snapshots: fold subtracts the edit and every total follows.
Worth trying
- Open Budget and change one month’s rent: about two dozen of its 333 formulas recalculate, in six rounds as the totals that read totals settle.
- Fill a column with numbers and put a SUM under it, then change one: the caption shows how few formulas were recomputed.
- Make a formula refer to itself: it shows #CYCLE!, and Undo brings the old value back.
- Drag the history slider to scrub the whole sheet through time.
Still app code
Formulas beyond a weighted sum (IF, comparisons, A1*B1) are evaluated by the app, but inside a fold aggregate, and only for the formulas whose inputs changed. Fold has no recursion yet, so formulas that read other formulas settle in rounds, one commit per level, and the app finds cycles. Fill, sort and inserting rows rewrite references in code.
Press \ in the app to watch fold's commits as they happen.