2.4 Day-to-day: a packing slip
Page 5 of 9
2.4.1 What the program must do
A packing slip goes into every box the store ships. It names the order and the customer, lists each line of the order with its quantity, counts the units and the boxes, and gives the total paid. The warehouse prints it as the box is packed.
The design follows from the job and from what you have learned. Each line of the slip comes from its own function, which takes the values the line shows and gives back its text. Those functions have no uses line, so they print nothing, and an example can test each one by its result. main names the sample order's values with let, calls the functions, and prints what they give back, line by line.
2.4.2 The slip's package and manifest
The program is one module, slip, in a package of its own, made as in Each step is a package, with this manifest:
[package]
name = "slip"
version = "0.1.0"
edition = "0.2"
[run]
entry = "slip"
grants = ["console"]2.4.3 The lines of the slip
Nine functions, each with a doc comment, each written in the short form, on one line:
--- The first line of a slip: the order the box holds. The warehouse keeps an order's
--- number as an unsigned 32-bit number, so it is a `U32`.
pub title(order: U32) -> Text = "Packing slip for order {order}"
--- Where the box goes: the customer's number, a `U32` as well, and email address.
pub ship_to(customer: U32, email: Text) -> Text = "Ship to customer {customer} <{email}>"
--- One line of the order: how many units of a SKU, and what the SKU is.
pub item(quantity: Int, sku: Text, name: Text) -> Text = " {quantity} x {sku} {name}"
--- What one line of the order costs, in cents.
pub line_cents(quantity: Int, cents: Int) -> Int = quantity * cents
--- How many boxes the units need, at most two units to a box: half the units, rounded up.
pub boxes(units: Int) -> Int = (units + 1) / 2
--- How much the order holds.
pub count_line(units: Int, lines: Int) -> Text = "{units} units in {lines} lines"
--- How many boxes the order ships in.
pub box_line(count: Int) -> Text = "Boxes in this shipment: {count}"
--- What the customer paid for the goods, in cents.
pub total_line(cents: Int) -> Text = "Total paid: {cents} cents"
--- The last line, the same on every slip.
pub closing() -> Text = "Questions? Write to orders@example.invalid and quote the order number."Each function uses an idea of this chapter:
titleandship_totake aU32. The warehouse keeps the numbers of orders and customers in 32 bits, so their type says so, and a number that does not fit is refused at the call.item,count_line,box_lineandtotal_lineput their parameters into a text with interpolation.itemstarts with two spaces, inside the text, so that the item lines stand out on paper.line_centsgives back anInt, not a text: it multiplies twoIntvalues with*.boxesdivides with/, which drops the remainder. Adding 1 before dividing by 2 turns "half, rounded down" into "half, rounded up": 3 units need 2 boxes, and 4 units need 2 as well.closingtakes no parameters and always gives back the same text.
Money is in cents, as an Int: 1990 is 19 units of money and 90 cents. Whole cents never round, so the total is exact. Exercise 3 prints them with two decimals.
2.4.4 Printing the slip
main names the order's values, computes what follows from them, then prints the slip one line at a time:
--- Prints the slip of order 1042: two desk lamps and one rug.
pub main() -> Unit
uses @console: Console[print]
=
let order: U32 = 1042
let lamps = 2
let rugs = 1
let units = lamps + rugs
let lamp_cents = line_cents(lamps, cents: 1990)
let rug_cents = line_cents(rugs, cents: 4500)
@console.print(title(order))
@console.print(ship_to(318, email: "customer318@example.invalid"))
@console.print(item(lamps, sku: "LAMP-01", name: "Desk lamp, brass"))
@console.print(item(rugs, sku: "RUG-07", name: "Wool rug, 120 by 180 cm"))
@console.print(count_line(units, lines: 2))
@console.print(box_line(boxes(units)))
@console.print(total_line(lamp_cents + rug_cents))
@console.print(closing())Read it in two parts:
- The six
letlines name the order's values.orderis aU32, astitletakes.unitsis the sum of the two quantities, andlamp_centsandrug_centsare whatline_centsgives back for each line. - The eight
printlines print the slip, each one calling the function of its line.box_line(boxes(units))callsboxesfirst, and passes its result tobox_line.
ship_to(318, …) passes a literal where a U32 is expected, so 318 is a U32. The calls with more than one argument label every argument after the first, as in chapter 1: line_cents(lamps, cents: 1990).
2.4.5 Why one function per line of the slip
Why so many small functions for a program this size? Each one is a line of the slip that someone may want to change: the wording of the closing line, the layout of an item, the size of a box. A change to one of them touches one line of code and its examples, and the header of each says, without a look at its body, that it only computes a text or a number. The same program written as one main with every text in place would be shorter, and nothing in it could be tested except by reading what it prints.
2.4.6 Running it
In the day-to-day directory, una run . prints the slip shown in What you'll build: the order and the customer, two item lines, three units in two lines, two boxes, and a total of 8480 cents, which is 2 × 1990 + 4500.
2.4.7 The whole program
The whole file, to read in one piece or to copy:
module slip
-- A packing slip: the sheet that goes into the box with an order, so that the customer
-- can check what arrived. This version prints the slip of one sample order.
--- The first line of a slip: the order the box holds. The warehouse keeps an order's
--- number as an unsigned 32-bit number, so it is a `U32`.
pub title(order: U32) -> Text = "Packing slip for order {order}"
--- Where the box goes: the customer's number, a `U32` as well, and email address.
pub ship_to(customer: U32, email: Text) -> Text = "Ship to customer {customer} <{email}>"
--- One line of the order: how many units of a SKU, and what the SKU is.
pub item(quantity: Int, sku: Text, name: Text) -> Text = " {quantity} x {sku} {name}"
--- What one line of the order costs, in cents.
pub line_cents(quantity: Int, cents: Int) -> Int = quantity * cents
--- How many boxes the units need, at most two units to a box: half the units, rounded up.
pub boxes(units: Int) -> Int = (units + 1) / 2
--- How much the order holds.
pub count_line(units: Int, lines: Int) -> Text = "{units} units in {lines} lines"
--- How many boxes the order ships in.
pub box_line(count: Int) -> Text = "Boxes in this shipment: {count}"
--- What the customer paid for the goods, in cents.
pub total_line(cents: Int) -> Text = "Total paid: {cents} cents"
--- The last line, the same on every slip.
pub closing() -> Text = "Questions? Write to orders@example.invalid and quote the order number."
--- Prints the slip of order 1042: two desk lamps and one rug.
pub main() -> Unit
uses @console: Console[print]
=
let order: U32 = 1042
let lamps = 2
let rugs = 1
let units = lamps + rugs
let lamp_cents = line_cents(lamps, cents: 1990)
let rug_cents = line_cents(rugs, cents: 4500)
@console.print(title(order))
@console.print(ship_to(318, email: "customer318@example.invalid"))
@console.print(item(lamps, sku: "LAMP-01", name: "Desk lamp, brass"))
@console.print(item(rugs, sku: "RUG-07", name: "Wool rug, 120 by 180 cm"))
@console.print(count_line(units, lines: 2))
@console.print(box_line(boxes(units)))
@console.print(total_line(lamp_cents + rug_cents))
@console.print(closing())
example "the title names the order"
do title(1042)
expect result == "Packing slip for order 1042"
example "the address line holds the customer number and the email"
do ship_to(318, email: "customer318@example.invalid")
expect result == "Ship to customer 318 <customer318@example.invalid>"
example "an item line is indented and starts with the quantity"
do item(2, sku: "LAMP-01", name: "Desk lamp, brass")
expect result == " 2 x LAMP-01 Desk lamp, brass"
example "a line costs its quantity times the unit price"
do line_cents(2, cents: 1990)
expect result == 3980
example "a line of zero units costs nothing"
do line_cents(0, cents: 1990)
expect result == 0
example "an odd number of units needs one box more than half of them"
do boxes(3)
expect result == 2
example "an even number of units fills every box"
do boxes(4)
expect result == 2
example "no units need no box"
do boxes(0)
expect result == 0
example "the count line gives units and lines"
do count_line(3, lines: 2)
expect result == "3 units in 2 lines"
example "the box line gives the number of boxes"
do box_line(2)
expect result == "Boxes in this shipment: 2"
example "the total is in cents"
do total_line(8480)
expect result == "Total paid: 8480 cents"Next:2.5 Testing it