Lesson 11 — Capstone: the giving ledger, whole
You have been building this book since Lesson 1 — one gift said out loud, then a total, then gifts with names, then tools, then finding, then papers. One trick remains, and then the book can be assembled entire.
Step 1: reading numbers back
The loose end from Lesson 6: a file holds text, so the cents in Feeding America|620 come back as the characters "620", not the number. But you own every tool needed to teach a program to read digits: part takes one character at a time, and find in the text "0123456789" turns a character into its value — "6" sits at place 6. Walk the characters, and build the number the way you would read it aloud: each new digit moves the value one place over (× 10) and adds in:
The book says it answers:
620 10000
Nothing new was needed — a loop from Lesson 2, two tools from Lesson 3, a function from Lesson 4. That is what a capstone is for: discovering that the pieces you already own compose into the thing you thought you couldn't do.
Your turn. Use digits to read "500" and say it with 5 added — the answer must be the number 505, not the text "5005".
The worked answer — try yours first, then peek
The book says it answers:
505
Step 2: the book loads and saves itself
Now the ledger's papers become a true round trip. Two tools carry it: loadgifts reads ledger.txt if it is there — splitting it into lines, splitting each line at its bar, reading the cents back with digits — and starts empty, kindly, if it is not. savegifts writes the book back in the same shape:
The book says it answers:
A brand-new ledger. Feeding America: 620 cents
Run it twice on your machine. The second run says no "A brand-new ledger." — the book was there, it loaded, and now it holds two gifts to Feeding America. The book remembers. That small disappearance of one line is the whole of Lesson 6, paying off.
Step 3: the whole book
Everything, assembled. The tools from Lessons 4, 5, and 11; the papers from Lesson 6; the kindness of Lesson 7; ready to be compiled by Lesson 8 into a program that stands on its own. On its first run the book seeds itself with two opening gifts; on every later run it simply opens where it left off:
The book says it answers:
A brand-new ledger. Barry Cunningham: 500 cents No gift to Noki is in the book. Feeding America: 620 cents Barry Cunningham: 500 cents Total: 1120 cents
Read it once, top to bottom, slowly. There is nothing in it you did not learn in a small step, and nothing in it you could not now change with confidence: a new kind of report, a new question for the book to answer, a kinder message. That is the test of a course — not that you can repeat its programs, but that the programs have become yours.
Compile it, as Lesson 8 taught, and it stands on its own:
./niflac ledger.nifla -o ledger ./ledger
Your turn
The book is yours now. In the final program, add one more opening gift — "Mother of Mercy", 250 cents — to the seeding (and save the book after seeding, as the program already does). Before you run it, say what the new total will be. Then run it and see.
The new total is 1120 + 250 = 1370 cents, reckoned before running — and the program agrees:
The worked answer — try yours first, then peek
The book says it answers:
A brand-new ledger. Barry Cunningham: 500 cents No gift to Noki is in the book. Feeding America: 620 cents Barry Cunningham: 500 cents Mother of Mercy: 250 cents Total: 1370 cents
You can now…
- read numbers out of text with a
digitstool built
from part and find;
- load and save a whole book of records as text lines,
round trip, surviving between runs;
- assemble everything — keeping, deciding, repeating,
lists, objects, tools, kinds, nothing, papers, attempts — into one real program, and compile it to stand on its own.
---
Where to go next
You have walked the whole path once. The shelf is still there when you need it: PRIMER.md keeps every word and tool of the language, one at a time, and the README in this repository is the language's full record — every stone of its building, in order, with the honest limits named. The suites (examples/, checker-examples/, floor-examples/, bare-examples/) are hundreds of small true programs to read and borrow from.
Build something small that is yours: a garden log, a verse keeper, a book of your own keeping. Keep it exact, keep it kind, and when the checker stops you at the door, read its words slowly — it is on your side.
And the promise from the opening, kept: you wrote like JavaScript the whole way — type, run, read — and your words ran with Swift's strength — checked, compiled, standing on their own. And the language you now hold is an operating system's language. Whatever you build next with it, you are building in the real thing.
Welcome to Nifla.