Home / Learn / 11 Packages and modules

Release v0.4.0Edition 0.2

11.7 Exercises

Page 8 of 9

The exercises come with the chapter's download, 11-packages.tar.gz. Each one starts from exercises/N/start: the day-to-day package with the exercise's examples added, and a stub of what to write when there is one. una test . fails there until the exercise is done. Run una fmt . before una check ., so the layout is canonical.

11.7.1 Exercise 1: a module for shipping

The invoice gains a total with shipping, 4.90 below 50.00 and nothing from 50.00 on. It imports shipping_fee from store.shipping, a module that does not exist yet:

exercises/1/start/src/store/invoice.unauna
--- What an order costs with shipping. Exercise 1: write `store.shipping`.
pub with_shipping(subtotal: Int) -> Int = subtotal + shipping_fee(subtotal)
exercises/1/start/src/store/invoice.unauna
example "an order of 49.99 pays 4.90 of shipping"
  do with_shipping(4999)
  expect result == 5489

example "an order of 50.00 ships for free"
  do with_shipping(5000)
  expect result == 5000

una test . stops at the check until the module exists:

Diagnostic
error U1001 at src/store/invoice.una:6:8: module `store.shipping` is not a module of this package; the standard modules that can be imported are `std.host` and `std.testing`
error U1001 at src/store/invoice.una:31:54: cannot find name `shipping_fee` in this module, its imports or the prelude

Create the file, write its module line, and write the function. Amounts are in cents, so 50.00 is 5000 and 4.90 is 490.

Hint: the module store.shipping lives in src/store/shipping.una, and its function must be pub for the invoice to import it. One if … then … else … on one line is enough.

Check your answer with una test ..

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

Add the module:

exercises/1/answer/src/store/shipping.unacheckruntestuna
module store.shipping

--- The shipping fee of an order, in cents: 4.90, and nothing from 50.00 on.
pub shipping_fee(subtotal: Int) -> Int = if subtotal >= 5000 then 0 else 490

5000 is the first subtotal that ships for free, so the test is >=, and both examples pass.

11.7.2 Exercise 2: show what a member saves

The invoice wants a note with the discount on one unit, such as members save 1.99. The start package has saving_note with an empty text, and this example:

exercises/2/start/src/store/invoice.unauna
example "a note says what a member saves on one unit"
  do saving_note(Product(sku: "LAMP-01", name: "Desk lamp", cents: 1990))
  expect result == "members save 1.99"
Output
example store.invoice "a note says what a member saves on one unit" ... FAILED
  src/store/invoice.una:46: expect result == "members save 1.99"
  it is false, with `result` = `""`

The report shows what the stub returns, an empty text, next to the expectation.

Hint: store.pricing already computes the discount, in a private function. Importing it gives the U1004 of section 11.2. Decide that it belongs to the module's interface, and say so in the declaration. Then format turns the cents into text.

Check your answer with una test ..

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

Make discount public in store.pricing:

src/store/pricing.unadiff
--- a/src/store/pricing.una
+++ b/src/store/pricing.una
@@ -14,3 +14,3 @@
 --- The members' discount, rounded down to a whole cent.
-discount(cents: Cents) -> Int
+pub discount(cents: Cents) -> Int
   ensures result >= 0 and result <= cents

Then import it in the invoice, and use it:

src/store/invoice.unadiff
--- a/src/store/invoice.una
+++ b/src/store/invoice.una
@@ -4,3 +4,3 @@
 import store.money (format)
-import store.pricing (line_cents, member_price)
+import store.pricing (discount, line_cents, member_price)
 
@@ -28,4 +28,4 @@
 
---- What a member saves on one unit. Exercise 2: replace the empty text.
-pub saving_note(product: Product) -> Text = ""
+--- What a member saves on one unit.
+pub saving_note(product: Product) -> Text = "members save {format(discount(product.cents))}"
 

discount now belongs to the interface of store.pricing: any module may call it, and changing its header later affects them all.

11.7.3 Exercise 3: a members' price that both modules need

The money module gains a helper that writes a members' price as text, format_member, with an example of its own. To call member_price, store.money now imports store.pricing, and the package no longer checks:

exercises/3/start/src/store/money.unauna
module store.money

import store.pricing (member_price)

--- An amount of money in cents, never below zero.
pub kind Cents = Int where it >= 0

--- An amount in cents as a person reads it: 1990 is "19.90", and -150 is "-1.50".
pub format(cents: Int) -> Text = if cents < 0 then "-{digits(0 - cents)}" else digits(cents)

--- What a member pays, as a person reads it.
pub format_member(cents: Cents) -> Text = format(member_price(cents))
exercises/3/start/src/store/money.unauna
example "a member's price, as a person reads it"
  do format_member(1990)
  expect result == "17.91"
Diagnostic
error U1003 at src/store/money.una:3:8: import cycle between modules `store.money` and `store.pricing`

Make the package check again, and keep format_member and its example, so that una test . runs all nine examples and they pass.

Hint: store.pricing already imports store.money, for Cents. Ask which module can see both format and member_price without a new cycle, and move the function there, with its example. Then store.money needs no import at all.

Check your answer with una test ..

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

Take the helper and its example out of store.money, with the import it needed:

src/store/money.unadiff
--- a/src/store/money.una
+++ b/src/store/money.una
@@ -1,4 +1,2 @@
 module store.money
-
-import store.pricing (member_price)
 
@@ -9,5 +7,2 @@
 pub format(cents: Int) -> Text = if cents < 0 then "-{digits(0 - cents)}" else digits(cents)
-
---- What a member pays, as a person reads it.
-pub format_member(cents: Cents) -> Text = format(member_price(cents))
 
@@ -30,5 +25 @@
   expect result == "-1.50"
-
-example "a member's price, as a person reads it"
-  do format_member(1990)
-  expect result == "17.91"

Put them in store.pricing, which takes format from store.money next to Cents:

src/store/pricing.unadiff
--- a/src/store/pricing.una
+++ b/src/store/pricing.una
@@ -2,3 +2,3 @@
 
-import store.money (Cents)
+import store.money (Cents, format)
 
@@ -19,2 +19,5 @@
 
+--- What a member pays, as a person reads it.
+pub format_member(cents: Cents) -> Text = format(member_price(cents))
+
 example "members pay 10 percent less"
@@ -26 +29,5 @@
   expect result == 199
+
+example "a member's price, as a person reads it"
+  do format_member(1990)
+  expect result == "17.91"

The imports again go one way only: store.pricing uses store.money, and store.money uses nothing. The example moved with the function it tests, so it is now listed under store.pricing:

Output
example store.pricing "members pay 10 percent less" ... ok
example store.pricing "the discount rounds down" ... ok
example store.pricing "a member's price, as a person reads it" ... ok

9 examples: 9 passed, 0 failed

Next:11.8 Recap and reference