Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

25 lines (21 sloc) 0.852 kb
{-# LANGUAGE BangPatterns, ScopedTypeVariables #-}
import Control.Monad
import qualified Data.ByteString as B
import Data.Csv.Incremental
import System.Exit
import System.IO
main :: IO ()
main = withFile "salaries.csv" ReadMode $ \ csvFile -> do
let loop !_ (Fail _ errMsg) = putStrLn errMsg >> exitFailure
loop acc (Partial k) = loop acc =<< feed k
loop acc (Some rs k) = loop (acc + sumSalaries rs) =<< feed k
loop acc (Done rs) = putStrLn $ "Total salaries: " ++
show (sumSalaries rs + acc)
feed k = do
isEof <- hIsEOF csvFile
if isEof
then return $ k B.empty
else k `fmap` B.hGetSome csvFile 4096
loop 0 (decode False)
where
sumSalaries rs = sum [salary | Right (_ :: String, salary :: Int) <- rs]
Jump to Line
Something went wrong with that request. Please try again.