Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 24 lines (20 sloc) 0.801 kb
c147a98 @tibbe Add example for incremental parser
authored
1 {-# LANGUAGE BangPatterns, ScopedTypeVariables #-}
2
3 import Control.Monad
4 import qualified Data.ByteString as B
5 import Data.Csv.Incremental
6 import System.Exit
7 import System.IO
8
9 main :: IO ()
10 main = withFile "salaries.csv" ReadMode $ \ csvFile -> do
11 let loop !_ (Fail _ errMsg) = putStrLn errMsg >> exitFailure
08ca035 @tibbe Update examples to follow changes to incremental API
authored
12 loop acc (Many rs k) = loop (acc + sumSalaries rs) =<< feed k
c147a98 @tibbe Add example for incremental parser
authored
13 loop acc (Done rs) = putStrLn $ "Total salaries: " ++
14 show (sumSalaries rs + acc)
15
16 feed k = do
17 isEof <- hIsEOF csvFile
18 if isEof
19 then return $ k B.empty
ab58993 @tibbe Improve example
authored
20 else k `fmap` B.hGetSome csvFile 4096
a7b222a @tibbe Use enum to indicate if CSV data has header
authored
21 loop 0 (decode NoHeader)
c147a98 @tibbe Add example for incremental parser
authored
22 where
23 sumSalaries rs = sum [salary | Right (_ :: String, salary :: Int) <- rs]
Something went wrong with that request. Please try again.