Capstone A: streaming the input

A lazy file is a stream

readFile returns the contents lazily: a line is read when the program asks for it and not before, so a fold over module Main (main) where import Data.Maybe (mapMaybe) csv :: String csv = unlines ["tea,3,250", "cake,1,400", "tea,2,250"] parseLine :: String -> Maybe (String, Int, Int) parseLine line = case break (== ',') line of (item, ',' : rest) -> case break (== ',') rest of (qty, ',' : pence) -> case (reads qty, reads pence) of ([(q, "")], [(p, "")]) -> Just (item, q, p) _ -> Nothing _ -> Nothing _ -> Nothing main :: IO () main = do writeFile "/tmp/sales-stream.csv" csv text <- readFile "/tmp/sales-stream.csv" let rows = mapMaybe parseLine (lines text) print (length rows) print (sum [qty * pence | (_, qty, pence) <- rows])

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

3
1650

Why the streaming matters

The parser here is written with break and Data.Text, so the program works on a lazy

The command line next

A report processor is run by a person or a script, so it needs arguments: a subcommand for the total and one for the count, a usage message and a non-zero status when the arguments are wrong. The next lesson adds exactly that to this program.

Questions

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

  1. What does a lazy readFile do?

    Answer

    Reads the file as the program demands it, line by line

    The contents are a lazy list of characters, consumed as the program uses them.

  2. What does mapMaybe parseLine do?

    Answer

    Keeps the parsed rows and drops the Nothing ones

    mapMaybe is map plus filter for a function that returns Maybe.

  3. Why does the heap stay small?

    Answer

    One line plus the accumulator are live at a time

    Laziness lets the consumer drive the producer of lines.

  4. What does this program print? module Main (main) where main :: IO () main = print (sum [3 * 250, 1 * 400, 2 * 250 :: Int])

    Answer

    1650

    750 plus 400 plus 500 is 1650.

  5. Which function reads standard input as a lazy string?

    Answer

    getContents

    getContents is the stdin version of the lazy readFile.

  6. What does this program print? module Main (main) where import Data.Maybe (mapMaybe) main :: IO () main = print (mapMaybe (\n -> if n > 2 then Just n else Nothing) [1, 2, 3, 4 :: Int])

    Answer

    [3,4]

    Only 3 and 4 pass the test and the Nothings are dropped.

  7. Why is the parser written without Data.Text here?

    Answer

    The input is a lazy String and nothing has to be unpacked per line

    The streaming version keeps the lazy String all the way through.

  8. What does this program print? module Main (main) where main :: IO () main = print (length (lines "a\nb\nc\nd"))

    Answer

    4

    Four lines, and the trailing newline adds no empty line.

  9. What is the check that the stream dropped no row?

    Answer

    The count and the total are the same as the list version

    Both versions give 3 and 1650, which is the invariant of the refactor.

  10. What does this program print? module Main (main) where main :: IO () main = print (read "42" :: Int)

    Answer

    42

    read parses the string and print shows the number.

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