Capstone C: results and totals

Results and totals

The pool of the previous lesson returns its results in completion order, which is a real order and not the order of the jobs. The program keeps the pairs of an id and a value, sorts them by id for the report, and prints the count and the sum as well, so a reader can check the table against a total.

let ordered = sortOn fst values mapM_ print ordered print (length ordered) print (sum (map snd ordered))

The count and the sum are computed from the same list, so they cannot disagree with the rows above them, which is the same rule the CSV report of the first capstone followed.

The program

module Main (main) where import Control.Concurrent (forkIO) import Control.Concurrent.STM import Control.Monad (replicateM_, when) import Data.List (sortOn) data Job = Job { jobId :: Int , jobCost :: Int } readJob :: TVar [Job] -> STM (Maybe Job) readJob queue = do jobs <- readTVar queue case jobs of [] -> pure Nothing job : rest -> writeTVar queue rest >> pure (Just job) worker :: TVar [Job] -> TVar [(Int, Int)] -> TVar Int -> IO () worker queue results finished = loop where loop = do next <- atomically (readJob queue) case next of Nothing -> atomically (modifyTVar' finished (+ 1)) Just job -> do atomically (modifyTVar' results ((jobId job, jobCost job * 2) :)) loop main :: IO () main = do queue <- newTVarIO [Job 1 5, Job 2 7, Job 3 3] results <- newTVarIO [] finished <- newTVarIO 0 replicateM_ 3 (forkIO (worker queue results finished)) atomically $ do done <- readTVar finished when (done < 3) retry values <- readTVarIO results let ordered = sortOn fst values mapM_ print ordered print (length ordered) print (sum (map snd ordered))

ghc -Wall -O1 -threaded -o results results.hs
./results

(1,10)
(2,14)
(3,6)
3
30

Reading the output

The next lesson adds a stop flag and an idle worker that waits on it.

Questions

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

  1. Why sort the results by id?

    Answer

    The completion order varies and the report must be stable

    The id restores the job order for the reader.

  2. Where do the count and the sum come from?

    Answer

    The same list that is printed

    One source keeps the totals consistent with the rows.

  3. What does printing the raw result list show?

    Answer

    The completion order, which changes between runs

    The runtime decides which worker finishes first.

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

    Answer

    30

    10 plus 14 plus 6 is 30.

  5. What does sortOn fst order the pairs by?

    Answer

    The job id

    fst of the pair is the id.

  6. What does this program print? module Main (main) where main :: IO () main = print (length [(1, 2), (3, 4)])

    Answer

    2

    The list has two pairs.

  7. Why is the count printed next to the rows?

    Answer

    It is the check that no result was lost

    A missing row shows up as a wrong count.

  8. What does this program print? module Main (main) where main :: IO () main = print (1 + 1 :: Int)

    Answer

    2

    The sum is printed without quotes.

  9. What is the sum of the values 10, 14 and 6?

    Answer

    30

    10 and 14 and 6 add to 30.

  10. What does this program print? module Main (main) where main :: IO () main = print (null [] :: Bool)

    Answer

    True

    The empty list is null.

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