Home / Learn / 15 Concurrency and parallelism

Release v0.4.0Edition 0.2

15.7 Exercises

Page 8 of 9

The exercises come with the chapter's download, 15-concurrency.tar.gz. Each one starts from exercises/N/start: the day-to-day program with the exercise's example added, and a stub of the function to write, a header with a placeholder body. una test . fails there until the exercise is done. Check your work with una check . and una test ..

15.7.1 Exercise 1: the SKUs of an order

Write skus_of(lines), which gives back the SKU of each line of an order, in the order of the lines, with together for. The start package has the stub and its example:

exercises/1/start/src/availability.unauna
--- The SKU of each line of an order, in the order of the lines.
pub skus_of(lines: List[OrderLine]) -> List[Text] = []
exercises/1/start/src/availability.unauna
example "the SKUs come in the order of the lines"
  do skus_of([OrderLine(sku: "LAMP-01", quantity: 5), OrderLine(sku: "DESK-02", quantity: 2)])
  expect result == ["LAMP-01", "DESK-02"]

Hint: the body is a together for over lines, and the last line of its body is the SKU of the line.

On the start package, una test . reports:

Output
example availability "the SKUs come in the order of the lines" ... FAILED
  src/availability.una:184: expect result == ["LAMP-01", "DESK-02"]
  it is false, with `result` = `[]`

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

Walk the lines, and give back each one's SKU:

src/availability.unadiff
--- a/src/availability.una
+++ b/src/availability.una
@@ -61,3 +61,6 @@
 --- The SKU of each line of an order, in the order of the lines.
-pub skus_of(lines: List[OrderLine]) -> List[Text] = []
+pub skus_of(lines: List[OrderLine]) -> List[Text]
+=
+  together for line in lines
+    line.sku
 

The function is pure, and asks no warehouse. For a short computation like this one, lines.map(each: fn(line) -> line.sku) would do the same, as Building a list at the same time says.

15.7.2 Exercise 2: counts that record a silent warehouse as none

A clerk wants to see what each warehouse holds of a line's SKU, even when one of them does not answer. Write partial(line), which asks the three warehouses at the same time and gives back an Availability, as availability does, but records 0 in the field of a warehouse that does not answer instead of failing. The start package has the stub, and an example in which the south warehouse is offline:

exercises/2/start/src/availability.unauna
--- What each warehouse holds of a line's SKU, all three asked at the same time. A warehouse
--- that does not answer is recorded as holding none.
pub partial(line: OrderLine) -> Availability
  uses @north: Warehouse[on_hand]
  uses @south: Warehouse[on_hand]
  uses @east: Warehouse[on_hand]
=
  Availability(sku: line.sku, wanted: line.quantity, north: 0, south: 0, east: 0)
exercises/2/start/src/availability.unauna
example "a warehouse that does not answer is recorded as holding none"
  given @north = shelves(north_stock())
  given @south = offline("south")
  given @east = shelves(east_stock())
  do partial(OrderLine(sku: "DESK-02", quantity: 2))
  expect result == Availability(sku: "DESK-02", wanted: 2, north: 1, south: 0, east: 1)

Hint: start from the together block of availability. Change each branch's clause so that a warehouse that does not answer gives 0, and build the record on the line after the block.

On the start package, una test . reports:

Output
example availability "a warehouse that does not answer is recorded as holding none" ... FAILED
  src/availability.una:193: expect result == Availability(sku: "DESK-02", wanted: 2, north: 1, south: 0, east: 1)
  it is false, with `result` = `Availability(sku: "DESK-02", wanted: 2, north: 0, south: 0, east: 0)`

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

A together block with a clause per branch that gives a default, then the record:

src/availability.unadiff
--- a/src/availability.una
+++ b/src/availability.una
@@ -67,3 +67,10 @@
 =
-  Availability(sku: line.sku, wanted: line.quantity, north: 0, south: 0, east: 0)
+  together
+    let north = @north.on_hand(line.sku)
+      if error use 0
+    let south = @south.on_hand(line.sku)
+      if error use 0
+    let east = @east.on_hand(line.sku)
+      if error use 0
+  Availability(sku: line.sku, wanted: line.quantity, north: north, south: south, east: east)
 

DESK-02 has 1 unit in the north warehouse and 1 in the east one. The south one does not answer, so its branch gives 0, and its field says 0. Every failure is handled in its branch, so the block never fails, and partial needs no fails in its header. Unlike estimate of section 15.1, which adds the counts, partial keeps each warehouse's count in its own field.

15.7.3 Exercise 3: every order of the day at once

At the end of the day, the store checks every open order at once. Write check_day(orders), which gives back, for each order, whether it ships whole, in the order of the orders. Every order is checked at the same time, and a warehouse that does not answer fails the whole day. The start package has the stub and its example:

exercises/3/start/src/availability.unauna
--- For each order of the day, whether it ships whole: every order checked at the same time.
pub check_day(orders: List[List[OrderLine]]) -> List[Bool]
  uses @north: Warehouse[on_hand]
  uses @south: Warehouse[on_hand]
  uses @east: Warehouse[on_hand]
  fails StockError
=
  []
exercises/3/start/src/availability.unauna
example "each order of the day gets its own answer, in order"
  given @north = shelves(north_stock())
  given @south = shelves(south_stock())
  given @east = shelves(east_stock())
  do check_day([[OrderLine(sku: "LAMP-01", quantity: 5)], [OrderLine(sku: "LAMP-01", quantity: 9)]])
  expect result == [true, false]

Hint: a together for over the orders, whose body checks one order with check_order and then calls ships_whole. check_order can fail, so its line needs a let and a clause.

Check your answer with una test ..

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

One task per order, and inside it, check_order's own tasks:

src/availability.unadiff
--- a/src/availability.una
+++ b/src/availability.una
@@ -67,3 +67,6 @@
 =
-  []
+  together for order in orders
+    let found = check_order(order)
+      if error pass
+    ships_whole(found)
 

The first order wants 5 lamps, and the warehouses hold 6: it ships. The second wants 9: it waits. Each task runs check_order, which runs a task per line, and each line asks three warehouses: tasks three levels deep, and the result is still the list a run one after another would give.

Next:15.8 Recap and reference