Home / Learn / 16 Memory

Release v0.4.0Edition 0.2

16.8 Exercises

Page 9 of 10

Each exercise starts from exercises/N/start: the day-to-day program with the exercise's example added, and a stub of what to write. una test . fails there until the exercise is done. Run una fmt . before una check .: canonical form, for example, puts a body of one line on the header line.

16.8.1 Exercise 1: a recount

A stocktake finds 7 lamps on the shelf where the ledger says 4. Write the body of recount, which sets the count of a SKU to what was counted. A recount is not a movement, so applied does not change, and the header promises it. The start package has the stub, a function for the example, and the example:

exercises/1/start/src/stockcount.unauna
--- A stocktake correction: sets the count of `sku` to the `units` counted on the shelf.
--- It is not a movement, so `applied` does not change. Exercise 1: write the body.
pub recount(edit ledger: Ledger, sku: Text, units: Int) -> Unit
  ensures ledger.applied == old(ledger).applied
=
  unit

--- `recount` through a copy, for the example.
recounted(ledger: Ledger, sku: Text, units: Int) -> Ledger
=
  let edit copy = ledger
  recount(edit copy, sku: sku, units: units)
  copy
exercises/1/start/src/stockcount.unauna
example "a recount sets the count and is not a movement"
  do recounted(Ledger(site: "north", counts: empty_map().with(key: "LAMP-01", value: 4), applied: 2), sku: "LAMP-01", units: 7)
  expect result.counts.get(key: "LAMP-01") == Some(7)
  expect result.applied == 2

Hint: one assignment to ledger.counts, with the map's with. The verifier proves the ensures once the body leaves applied alone.

Check your answer with una test .. On the start package it reports:

Output
example stockcount "a recount sets the count and is not a movement" ... FAILED
  src/stockcount.una:150: expect result.counts.get(key: "LAMP-01") == Some(7)
  it is false, with `result` = `Ledger(site: "north", counts: {"LAMP-01": 4}, applied: 2)`

5 examples: 4 passed, 1 failed
Answer (check, run, test; exercises/1/answer).

Replace the body:

src/stockcount.unadiff
--- a/src/stockcount.una
+++ b/src/stockcount.una
@@ -134,3 +134,3 @@
 --- A stocktake correction: sets the count of `sku` to the `units` counted on the shelf.
---- It is not a movement, so `applied` does not change. Exercise 1: write the body.
+--- It is not a movement, so `applied` does not change.
 pub recount(edit ledger: Ledger, sku: Text, units: Int) -> Unit
@@ -138,3 +138,3 @@
 =
-  unit
+  ledger.counts = ledger.counts.with(key: sku, value: units)
 

16.8.2 Exercise 2: a delivery note, all or nothing

A delivery note lists several movements, and the warehouse accepts it whole or not at all. Write the body of apply_note, which applies every movement of a note to the ledger and gives one line per movement, and fails when any movement fails. The start package has the stub, a function for the examples, and two examples:

exercises/2/start/src/stockcount.unauna
--- Applies a delivery note, every movement or none, and gives one line per movement.
--- Exercise 2: write the body.
pub apply_note(edit ledger: Ledger, movements: List[Movement]) -> List[Text]
  fails CountError
=
  []

--- `apply_note` through a copy, for the examples: the ledger after the note, or before it.
after_note(ledger: Ledger, movements: List[Movement]) -> Ledger
=
  let edit copy = ledger
  let lines = apply_note(edit copy, movements: movements)
    if error use []
  copy
exercises/2/start/src/stockcount.unauna
example "a note whose movements all apply applies all of them"
  do after_note(lamps(4), movements: [delivery("LAMP-01", units: 6), sale("LAMP-01", units: 2)])
  expect result.counts.get(key: "LAMP-01") == Some(8)
  expect result.applied == 2

example "a note with one bad movement applies none of them"
  do after_note(lamps(4), movements: [delivery("LAMP-01", units: 6), sale("DESK-02", units: 1)])
  expect result.counts.get(key: "LAMP-01") == Some(4)
  expect result.applied == 0

The second example passes on the stub, which applies nothing. Your answer must keep it passing.

Hint: a for loop that calls apply with if error pass. You do not need to undo anything: when apply_note fails, the caller's ledger is left as it was before the call.

Check your answer with una test .. An answer that skips the bad movement with if error use fails the second example.

Answer (check, run, test; exercises/2/answer).

Replace the body:

src/stockcount.unadiff
--- a/src/stockcount.una
+++ b/src/stockcount.una
@@ -134,3 +134,2 @@
 --- Applies a delivery note, every movement or none, and gives one line per movement.
---- Exercise 2: write the body.
 pub apply_note(edit ledger: Ledger, movements: List[Movement]) -> List[Text]
@@ -138,3 +137,8 @@
 =
-  []
+  let edit lines: List[Text] = []
+  for movement in movements
+    let line = apply(edit ledger, movement: movement)
+      if error pass
+    lines = lines.append(item: line)
+  lines
 

A movement that fails ends the loop and the function. The ledger the loop changed is discarded with the failure, so the caller's ledger never holds half a note.

16.8.3 Exercise 3: a strict close

The store wants a stricter close: every movement of every site must apply, or the close fails and names the first site, in list order, that did not add up. The start package has strict_day, which fails at a site's first bad movement, a kind for the failure, a stub of close_strict, and blamed, which runs close_strict and names the site it blames, or none when it does not fail:

exercises/3/start/src/stockcount.unauna
--- Why a strict close failed: the first site, in list order, with a movement that did not apply.
pub kind CloseError = | Short(site: Text, cause: CountError)

--- Replays one site's day, and fails at the first movement that does not apply.
pub strict_day(day: Day) -> Report
  fails CountError
=
  let edit ledger = Ledger(site: day.site, counts: day.opening, applied: 0)
  let edit lines: List[Text] = []
  for movement in day.movements
    let line = apply(edit ledger, movement: movement)
      if error pass
    lines = lines.append(item: line)
  close(take ledger, lines: lines)

--- Closes every day at once, strictly. Exercise 3: use `strict_day`, one task per day.
pub close_strict(days: List[Day]) -> List[Report]
  fails CloseError
=
  close_all(days)

--- The site that `close_strict` blames, or "none".
blamed(days: List[Day]) -> Text
=
  let outcome = attempt close_strict(days)
  when outcome
    is Ok -> "none"
    is Err(failure) -> site_of(failure)

site_of(failure: CloseError) -> Text
=
  when failure
    is Short(site, cause) -> site
exercises/3/start/src/stockcount.unauna
example "a strict close blames the first short site in list order"
  do blamed([Day(site: "east", opening: empty_map(), movements: []), south_day(), north_day()])
  expect result == "south"

example "a strict close of days that all apply blames no site"
  do blamed([Day(site: "east", opening: empty_map().with(key: "LAMP-01", value: 1), movements: [sale("LAMP-01", units: 1)])])
  expect result == "none"

In the first example, both the south and the north days fail. The close must name south, the first of them in the list.

Hint: one task per day, as in close_all, with strict_day in each, and a clause that translates its failure into Short with the day's site. You do not need to find the first failure yourself.

Check your answer with una test ..

Answer (check, run, test; exercises/3/answer).

Replace the body:

src/stockcount.unadiff
--- a/src/stockcount.una
+++ b/src/stockcount.una
@@ -148,3 +148,3 @@
 
---- Closes every day at once, strictly. Exercise 3: use `strict_day`, one task per day.
+--- Closes every day at once, strictly: one task per day.
 pub close_strict(days: List[Day]) -> List[Report]
@@ -152,3 +152,5 @@
 =
-  close_all(days)
+  together for day in days
+    strict_day(day)
+      if error fail Short(site: day.site, cause: error)
 

Two tasks fail, and the block fails with the failure of the lowest-indexed one, as a sequential run would (section 15.3). In an executable, the tasks may finish in any order, and the answer is the same.

Next:16.9 Recap and reference