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
- A file of a gigabyte is read a line at a time, and the heap holds one line plus the accumulator.
- The same code with interact reads standard input, and the lesson on stdin and stdout showed that version.
- A strict readFile from a strict text type would hold the whole file, which is the trade the text lesson explained.
The parser here is written with break and
Data.Text, so
the program works on a lazy
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. A new set of questions in a new order every time. Choose one answer for each, then check. What does a lazy 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. What does Keeps the parsed rows and drops the Nothing ones mapMaybe is map plus filter for a function that returns Maybe. Why does the heap stay small? One line plus the accumulator are live at a time Laziness lets the consumer drive the producer of lines. What does this program print?
1650 750 plus 400 plus 500 is 1650. Which function reads standard input as a lazy string? getContents is the stdin version of the lazy readFile. What does this program print?
[3,4] Only 3 and 4 pass the test and the Nothings are dropped. Why is the parser written without Data.Text here? 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. What does this program print?
4 Four lines, and the trailing newline adds no empty line. What is the check that the stream dropped no row? 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. What does this program print?
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
The command line next
Questions
readFile do?Answer
mapMaybe parseLine do?Answer
Answer
module Main (main) where
main :: IO ()
main = print (sum [3 * 250, 1 * 400, 2 * 250 :: Int])Answer
Answer
getContentsmodule 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
Answer
module Main (main) where
main :: IO ()
main = print (length (lines "a\nb\nc\nd"))Answer
Answer
module Main (main) where
main :: IO ()
main = print (read "42" :: Int)Answer