Home / Learn / 7 Results and failures

Release v0.4.0Edition 0.2

7.10 Exercises

Page 11 of 12

The exercises come with the chapter's download, 07-failure.tar.gz. 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 when there is one, a header with a body that gives back a placeholder. una test . fails there until the exercise is done. Run una fmt . before una check .: it writes the code in the canonical form that una check expects.

7.10.1 Exercise 1: a per-line limit

The store ships at most 10 units of a SKU per line. Add a variant TooMany(sku: Text, limit: Int) and reject larger quantities in parse_order, before the stock list is asked. The start package ends with this example, and una check . reports U1001 until the variant exists:

exercises/1/start/src/orders.unauna
example "more than ten units on one line is TooMany"
  do parse_order("LAMP-01, 11")
  expect fails TooMany

Hint: work in three small steps, and run una check . after each. First add the variant TooMany(sku: Text, limit: Int) to OrderError: the checker then shows the one when that needs a new arm, in reason. Second, give reason that arm, with a sentence that uses both fields. Third, in parse_order, add one more else if branch after the test for a quantity below 1, which fails with TooMany when the quantity is above 10 (fail in a branch).

What una check . reports after adding only the variant:

Diagnostic
[U2007] Error: `when` does not match variant `TooMany` of `OrderError`
    │ Help: repair 1: add an arm for each missing variant, such as `is TooMany`, or an `else` arm

Check your answer with una test ..

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

Add the variant to OrderError, the limit to parse_order, and the arm to reason:

src/orders.unadiff
--- a/src/orders.una
+++ b/src/orders.una
@@ -8,3 +8,3 @@
 --- Why an order line was rejected. Each variant carries what a person needs to act on it.
-pub kind OrderError = Malformed(line: Text) | BadQuantity(line: Text) | UnknownSku(sku: Text) | OutOfStock(sku: Text, wanted: Int, left: Int) | Unavailable(sku: Text)
+pub kind OrderError = Malformed(line: Text) | BadQuantity(line: Text) | UnknownSku(sku: Text) | OutOfStock(sku: Text, wanted: Int, left: Int) | Unavailable(sku: Text) | TooMany(sku: Text, limit: Int)
 
@@ -38,2 +38,4 @@
     fail BadQuantity(line)
+  else if quantity > 10
+    fail TooMany(sku: sku, limit: 10)
   else
@@ -71,2 +73,3 @@
     is Unavailable(sku) -> "the shelf of {sku} is being counted; retry later"
+    is TooMany(sku, limit) -> "at most {limit} of {sku} per line"
 

una test . reports every example passing.

7.10.2 Exercise 2: a line with no SKU

The line , 2 has a comma and a quantity, but nothing before the comma. Today parse_order accepts it with an empty SKU, and the stock list then rejects it as an unknown SKU, with an empty name in the report. Make parse_order fail with Malformed when the SKU is empty. The start package ends with this example:

exercises/2/start/src/orders.unauna
example "a line with no SKU before the comma is Malformed"
  do parse_order(" , 2")
  expect fails Malformed

Hint: after the line that makes sku, test its length, sku.len == 0, with an if that has no else (section 3.4), and fail Malformed(line) in it.

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

Output
example orders "a line with no SKU before the comma is Malformed" ... FAILED
  src/orders.una:130: expect fails Malformed
  the `do` line completed with `Order(sku: "", quantity: 2)`

10 examples: 9 passed, 1 failed
Answer (check, run, test; exercises/2/answer).

Test the SKU right after it is made:

src/orders.unadiff
--- a/src/orders.una
+++ b/src/orders.una
@@ -34,2 +34,4 @@
   let sku = line.slice(start: 0, end: comma).trim
+  if sku.len == 0
+    fail Malformed(line)
   let quantity = line.slice(start: comma + 1, end: line.len).trim.to_int()

The test runs before the quantity is read, so a line with no SKU is Malformed, whatever follows the comma.

7.10.3 Exercise 3: the units a line ships

Write units_of(line): the quantity of an order line when accept accepts it, and 0 when it rejects it. The start package has it with a body of 0, and these examples:

exercises/3/start/src/orders.unauna
--- The units an order line ships: its quantity when it is accepted, 0 when it is rejected.
--- Exercise 3: replace the 0.
pub units_of(line: Text) -> Int = 0

example "an accepted line ships its quantity"
  do units_of("LAMP-01, 2")
  expect result == 2

example "a rejected line ships nothing"
  do units_of("DESK-02, 3")
  expect result == 0

Hint: units_of must not fail, so keep the outcome of accept with attempt (section 7.4), then take it apart with a when whose Ok arm gives the order's quantity and whose Err arm gives 0. The header changes to the long form, since the body has two steps.

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

Output
example orders "an accepted line ships its quantity" ... FAILED
  src/orders.una:134: expect result == 2
  it is false, with `result` = `0`
example orders "a rejected line ships nothing" ... ok

11 examples: 10 passed, 1 failed
Answer (check, run, test; exercises/3/answer).

Keep the outcome, and give 0 for a rejected line:

src/orders.unadiff
--- a/src/orders.una
+++ b/src/orders.una
@@ -128,4 +128,8 @@
 --- The units an order line ships: its quantity when it is accepted, 0 when it is rejected.
---- Exercise 3: replace the 0.
-pub units_of(line: Text) -> Int = 0
+pub units_of(line: Text) -> Int
+=
+  let outcome = attempt accept(line)
+  when outcome
+    is Ok(order) -> order.quantity
+    is Err -> 0
 

if error use would not do here: it needs a value of the type Order to carry on with, and no order stands for a rejected line. attempt keeps the outcome as a value, and the when decides what each case ships.

Next:7.11 Recap and reference