Each exercise starts from exercises/N/start and has a test that fails there until the exercise is done: an example in the package of exercise 1, of edition 0.1, and a test in the others, of edition 0.3. Work in the start package's directory: check the edition in una.toml first, then run una fmt ., una check . and una test ..
19.9.1 Exercise 1: move a package to 0.2
The edition 0.1 stock list now also counts the units sold, with a function whose parameters are old and new:
exercises/1/start/src/shelf.unauna
--- The units sold since the last count: the count `old` from before, less the count `new` now.sold(old: Int, new: Int) -> Int = old - new
Someone then added low_stock, with a loop, and an example for it. The package no longer checks. Move it to the edition that the loop needs, and change only what the move requires. The start package's new function and example:
exercises/1/start/src/shelf.unauna
--- The SKUs of the lines with fewer than `limit` units, in the order of the list.low_stock(lines: List[Line], limit: Int) -> List[Text]
=
letedit found: List[Text] = []
for line in lines
if line.units < limit
found = found.append(item: line.sku)
found
exercises/1/start/src/shelf.unauna
example"the lines below the limit are low on stock"dolow_stock([Line(sku: "LAMP-01", units: 4), Line(sku: "DESK-02", units: 1)], limit: 2)
expectresult == ["DESK-02"]
Diagnostic
errorU0103 at src/shelf.una:17:7: `edit` is reserved for v0.2 (memory model)
Hint: the loop is right; the message says which edition has edit. Read Letting una fix rename a keyword before you run una fix, then follow the steps of a move: after the edition line, run una check . again, since the new edition reads some names differently.
Check your answer with una check ., then una test ..
With the edition line alone, una check . reports U0101 at old: "old is a reserved keyword, so it cannot name a binding". una fix has no edit for it, since the new name is a choice, so rename it by hand:
src/shelf.unadiff
--- a/src/shelf.una+++ b/src/shelf.una@@ -10,4 +10,4 @@---- The units sold since the last count: the count `old` from before, less the count `new` now.-sold(old: Int, new: Int) -> Int = old - new+--- The units sold since the last count: the count `before`, less the count `new` now.+sold(before: Int, new: Int) -> Int = before - new
19.9.2 Exercise 2: a restock run
The night shift wants labels for the lines that are low on stock, so they can restock them first. Write the body of print_low: like print_labels, but for the lines with fewer than limit units, with the same dry run. The start package has the header with its condition, a stub body, and two tests:
exercises/2/start/src/labels.unauna
--- Prints a label for every line with fewer than `limit` units, and gives how many, for a--- restock run. A dry run prints nothing. Exercise 2: write the body.pubprint_low(lines: List[Line], limit: Int, dry_run: Bool) -> Intuses@printer: Printer[print_label wherenot dry_run]
=
0
exercises/2/start/src/labels.unauna
test "a restock run prints the lines below the limit"given@printer = silent()
doprint_low(read_lines(export()), limit: 5, dry_run: false)
expectresult == 2expectperformed@printer.print_label
test "a restock dry run prints nothing"given@printer = silent()
doprint_low(read_lines(export()), limit: 5, dry_run: true)
expectresult == 2expectnotperformed@printer.print_label
Hint: start from the body of print_labels, and change only the test on line.units. Keep the count outside the if not dry_run, so a dry run counts too. If you leave out the guard, una check . reports where, with a counterexample where dry_run is true.
Check your answer with una test ..
Answer (check, run, test; exercises/2/answer).
Replace the body:
src/labels.unadiff
--- a/src/labels.una+++ b/src/labels.una@@ -88,3 +88,3 @@
--- Prints a label for every line with fewer than `limit` units, and gives how many, for a
---- restock run. A dry run prints nothing. Exercise 2: write the body.+--- restock run. A dry run prints nothing.
pub print_low(lines: List[Line], limit: Int, dry_run: Bool) -> Int
@@ -92,3 +92,9 @@
=
- 0+ let edit printed = 0+ for line in lines+ if line.units < limit+ if not dry_run+ @printer.print_label(line.sku, units: line.units)+ printed = printed + 1+ printed
19.9.3 Exercise 3: a summary on every run
The printer gains a second operation, print_summary, which prints one line at the end of a run. The summary must be printed on every run, a dry run included, while labels stay forbidden in a dry run. The start package adds the operation to Printer:
exercises/3/start/src/labels.unauna
--- The shelf-label printer. `print_summary` prints a line at the end of a run, on paper.pubcapabilityPrinteropprint_label(sku: Text, units: Int) -> Unitopprint_summary(labels: Int) -> Unit
It adds it to both handlers:
exercises/3/start/src/labels.unauna
--- A printer that writes each label to the console.handleron_console() forPrinteruses@console: Console[print]
opprint_label(sku, units) = @console.print("[{sku} | {units} on hand]")
opprint_summary(labels) = @console.print("{labels} labels")
--- A printer that prints nothing, for the tests.handlersilent() forPrinteropprint_label(sku, units) = unitopprint_summary(labels) = unit
And it adds this test:
exercises/3/start/src/labels.unauna
test "a dry run prints the summary, and no label"given@printer = silent()
doprint_labels(read_lines(export()), dry_run: true)
expectperformed@printer.print_summary
expectnotperformed@printer.print_label
Hint: the header decides which operations may run, and under which condition: add print_summary to the uses clause of print_labels, with no condition, and call it after the loop. Once the code checks on its own, read what una check . says about una.toml: the lock must list what the header grants.
After the change to the code alone, una check . reports:
Diagnostic
[U3023]Error: the header of `print_labels` lets `@printer.print_summary` run, which its lock in `una.toml` does not list
Check your answer with una check ., then una test ..
Answer (check, run, test; exercises/3/answer).
Change the header and the body:
src/labels.unadiff
--- a/src/labels.una+++ b/src/labels.una@@ -39,6 +39,6 @@---- Prints a label for every line with stock, and gives how many. A dry run prints nothing.---- Exercise 3: print the summary at the end of every run, a dry run included.+--- Prints a label for every line with stock, and gives how many. A dry run prints no label,+--- and every run prints the summary.
pub print_labels(lines: List[Line], dry_run: Bool) -> Int
- uses @printer: Printer[print_label where not dry_run]+ uses @printer: Printer[print_label where not dry_run, print_summary]
=
@@ -50,2 +50,3 @@
printed = printed + 1
+ @printer.print_summary(printed)
printed
and the lock, which a reviewer sees:
una.tomldiff
--- a/una.toml+++ b/una.toml@@ -8,4 +8,4 @@
grants = ["console"]
[contracts]
-"labels.print_labels" = ["@printer: Printer[print_label where not dry_run]"]+"labels.print_labels" = ["@printer: Printer[print_label where not dry_run, print_summary]"]