Home / Learn / 3 Text and conditions

Release v0.4.0Edition 0.2

3.8 Exercises

Page 9 of 10

The exercises come with the chapter's download, 03-text-and-conditions.tar.gz. Each starts from exercises/N/start: the day-to-day program with the exercise's examples added, and, when the exercise asks for a new function, a stub of it, whose body is a placeholder value. una test . fails there until the exercise is done. Work as in chapter 1:

  1. Read the examples: they say what the function must give back.
  2. Change the code.
  3. Run una check .. When it refutes a cut or a division, read the counterexample: it is the input your code does not handle yet.
  4. Run una test . until every example passes, then una run . to see the labels.

3.8.1 Exercise 1: food or not

The store keeps food on its own shelves. Write is_food(sku), which is true when the SKU starts with COFFEE- or with TEA-. The start package ends with:

exercises/1/start/src/labels.unauna
--- Whether a SKU is one of the store's foods: its SKU starts with "COFFEE-" or "TEA-".
pub is_food(sku: Text) -> Bool = false

example "coffee is a food"
  do is_food("COFFEE-05")
  expect result

example "tea is a food"
  do is_food("TEA-02")
  expect result

example "a lamp is not"
  do is_food("LAMP-01")
  expect not result

Hint: the body is one condition: two calls of starts_with, joined by the operator that accepts either.

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

Output
example labels "coffee is a food" ... FAILED
  src/labels.una:120: expect result
  it is false, with `result` = `false`
example labels "tea is a food" ... FAILED
  src/labels.una:124: expect result
  it is false, with `result` = `false`
example labels "a lamp is not" ... ok

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

Give is_food its condition:

src/labels.unadiff
--- a/src/labels.una
+++ b/src/labels.una
@@ -115,3 +115,3 @@
 --- Whether a SKU is one of the store's foods: its SKU starts with "COFFEE-" or "TEA-".
-pub is_food(sku: Text) -> Bool = false
+pub is_food(sku: Text) -> Bool = sku.starts_with(prefix: "COFFEE-") or sku.starts_with(prefix: "TEA-")
 

The placeholder false passes the example for the lamp, but not the two for coffee and tea. With or, a SKU that starts with either prefix is a food.

3.8.2 Exercise 2: the last unit

When one unit is left on a shelf, the label should say last one, not only 1 left. Change stock_note, then print the label of a brass bookend in main, after the mug: SKU book-11, 2400 cents, no discount, no weight, one unit. The start package has a new example after the one for an empty shelf:

exercises/2/start/src/labels.unauna
example "a shelf with one unit left says so"
  do stock_note(1)
  expect result == "last one"

Hint: a third case goes between the two that stock_note has: an else if for units == 1.

Check your answer with una test ., then una run . for the new label.

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

Add the case to stock_note, and the label to main:

src/labels.unadiff
--- a/src/labels.una
+++ b/src/labels.una
@@ -41,3 +41,3 @@
 --- A note on the stock left, for a shelf with fewer than five units.
-pub stock_note(units: Int) -> Text = if units <= 0 then "sold out" else "only {units} left"
+pub stock_note(units: Int) -> Text = if units <= 0 then "sold out" else if units == 1 then "last one" else "only {units} left"
 
@@ -61,2 +61,3 @@
   @console.print(shelf_label("mug 4", name: "Tea mug, blue", cents: 650, percent: 0, grams: 0, units: 8))
+  @console.print(shelf_label("book-11", name: "Brass bookend", cents: 2400, percent: 0, grams: 0, units: 1))
 

The new label ends with last one. The conditions are tried in order, so an empty shelf is still sold out, and two units or more still say how many are left.

3.8.3 Exercise 3: three dots for a cut name

A cut name should show that it was cut. When a name is longer than 24 characters, keep its first 21 characters, remove the spaces at their end, and add ..., so that the label never grows past 24. The start package has two new examples in place of the one for long names:

exercises/3/start/src/labels.unauna
example "a long name ends with three dots"
  do fit_name("Wool rug, 120 by 180 cm, natural")
  expect result == "Wool rug, 120 by 180..."

example "a cut name still fits the label"
  do fit_name("Coffee beans, dark roast, whole")
  expect result.len <= 24

Hint: in the then branch, build a text with interpolation: the cut piece, trimmed at its end, then the three dots. A call that holds no text literal may stand inside the braces. Is a cut of 21 characters proved where the name has more than 24?

Check your answer with una test ., then una run ..

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

Change the then branch of fit_name:

src/labels.unadiff
--- a/src/labels.una
+++ b/src/labels.una
@@ -16,3 +16,3 @@
 --- A product name cut to the 24 characters a label has room for.
-pub fit_name(name: Text) -> Text = if name.len > 24 then name.slice(start: 0, end: 24) else name
+pub fit_name(name: Text) -> Text = if name.len > 24 then "{name.slice(start: 0, end: 21).trim_end}..." else name
 

The first 21 characters of the rug's name end with a space, Wool rug, 120 by 180 , so trim_end removes it before the dots. The cut is proved, since a name longer than 24 characters is longer than 21 too. The rug's label now shows Wool rug, 120 by 180....

Next:3.9 Recap and reference