Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example of incremental, name based parsing #209

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions examples/IncrementalNameBasedDecode.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{-# LANGUAGE BangPatterns, ScopedTypeVariables, OverloadedStrings #-}

import Control.Monad
import qualified Data.ByteString as B
import Data.Csv (FromNamedRecord(..), (.:))
import Data.Csv.Incremental
import System.Exit
import System.IO
import Data.Either (rights)

data SalaryInfo = SalaryInfo
{ name :: String
, salary :: Int
}

instance FromNamedRecord SalaryInfo where
parseNamedRecord m = SalaryInfo <$>
m .: "name" <*>
m .: "salary"

main :: IO ()
main = withFile "salaries.csv" ReadMode $ \ csvFile -> do

let headerLoop (FailH _ errMsg) = putStrLn errMsg >> exitFailure
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use die errMsg instead? (Also below.)

headerLoop (PartialH fn) = headerLoop =<< feedHeader fn
headerLoop (DoneH header parser) = loop 0 parser

loop !_ (Fail _ errMsg) = putStrLn errMsg >> exitFailure
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why !_ and not just _?
One possible explanation: There are errors inside acc that should be brought to surface before crashing. Is that true?

loop acc (Many rs k) = loop (acc + sumSalaries rs) =<< feed k
loop acc (Done rs) = putStrLn $ "Total salaries: " ++
show (sumSalaries rs + acc)

feedHeader k = do
isEof <- hIsEOF csvFile
if isEof
then return $ k B.empty
else k `fmap` B.hGetSome csvFile 4096
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use k <$> ... instead of k ``fmap`` ....


feed k = do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feed looks like an identical clone of feedHeader. Please get rid of one of them.

isEof <- hIsEOF csvFile
if isEof
then return $ k B.empty
else k `fmap` B.hGetSome csvFile 4096
headerLoop (decodeByName :: HeaderParser (Parser SalaryInfo))
where
sumSalaries :: [Either String SalaryInfo] -> Int
sumSalaries rs =
let good = rights rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens to the lefts? Is Left s an error message?
Usually, swallowing errors is not a good idea. These errors should be raised. (If these are errors.)

in sum $ map (\sinfo -> salary sinfo) good
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary lambda? Does sum $ map salary good also work?

9 changes: 9 additions & 0 deletions examples/cassava-examples.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ executable IncrementalIndexedBasedDecode
vector
default-language: Haskell2010

executable IncrementalNameBasedDecode
main-is: IncrementalNameBasedDecode.hs
build-depends:
base,
bytestring,
cassava,
vector
default-language: Haskell2010

executable IndexBasedDecode
main-is: IndexBasedDecode.hs
build-depends:
Expand Down
3 changes: 3 additions & 0 deletions examples/salaries.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name,salary
Paul,100
Sam,200