Capstone A: the report

The report

A report groups the rows by item, totals each group, sorts the groups by name and prints them as a table. Three library functions do the work: sortOn fst for the order and totals :: Map.Map String Int totals = Map.fromListWith (+) [(rowItem r, lineTotal r) | r <- rows]

Map is already ordered by key, and

The program

module Main (main) where import Data.List (sortOn) import qualified Data.Map.Strict as Map import Text.Printf (printf) data Row = Row { rowItem :: String , rowQty :: Int , rowPence :: Int } rows :: [Row] rows = [Row "tea" 3 250, Row "cake" 1 400, Row "tea" 2 250] totals :: Map.Map String Int totals = Map.fromListWith (+) [(rowItem r, rowQty r * rowPence r) | r <- rows] main :: IO () main = do printf "%-8s %8s\n" "item" "pence" mapM_ (uncurry (printf "%-8s %8d\n")) (sortOn fst (Map.toList totals)) printf "%-8s %8d\n" "total" (sum (Map.elems totals))

ghc -Wall -O1 -o report report.hs
./report

item         pence
cake            400
tea           1250
total         1650

The table and its columns

The widths in the format string are the whole design of the table: the name column is eight characters and left-aligned, the pence column is eight and right-aligned, and the header uses the same two. The rows line up because every line is formatted with the same widths, not because the data is short.

The total is printed with the same two widths, so it sits under the column it belongs to.

What the report checks

The numbers are checkable by hand: tea is three units at 250 plus two at 250, so 1250; cake is one at 400; the total is 1650. A report that prints a total different from the sum of its visible rows is the first bug a reader finds, which is why the total comes from the same map and not from a second pass over the rows.

The next lesson reads the file lazily, so the report of a file that does not fit in memory is still one pass.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does Map.fromListWith (+) do with duplicate keys?

    Answer

    Combines their values with +

    The combining function is applied to the old and the new value.

  2. Why is sortOn fst used when a Map is already ordered?

    Answer

    It makes the order explicit for the reader

    The map is ordered, and the sort states the order the report wants.

  3. What does %8d do?

    Answer

    Right-aligns an integer in eight characters

    The width is a minimum and the default alignment is right.

  4. What does this program print? module Main (main) where import qualified Data.Map.Strict as Map main :: IO () main = print (Map.toList (Map.fromListWith (+) [("a", 1), ("a", 2)]))

    Answer

    [("a",3)]

    The duplicate key is combined by + and the map has one entry.

  5. What is the group total for tea with rows 3 x 250 and 2 x 250?

    Answer

    1250 pence

    Three at 250 plus two at 250 is 1250.

  6. What does this program print? module Main (main) where main :: IO () main = print (sum [400, 1250 :: Int])

    Answer

    1650

    The two group totals add to 1650.

  7. Why does the total come from the map and not a second pass?

    Answer

    So the printed total cannot disagree with the printed rows

    One source for the numbers keeps the table consistent.

  8. What does this program print? module Main (main) where import Data.List (sortOn) main :: IO () main = print (sortOn fst [(2, "b"), (1, "a")])

    Answer

    [(1,"a"),(2,"b")]

    sortOn fst orders the pairs by their first component.

  9. Which function formats a fixed-width row?

    Answer

    Text.Printf.printf

    printf takes the widths and the values and returns the formatted line.

  10. What does %-8s do in the format string?

    Answer

    Left-aligns a string in eight characters

    The minus sign asks for left alignment and 8 is the minimum width.

The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev