HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulecassava-0.5.3.2Haskell2010

Data.Csv.Incremental

This module allows for incremental decoding and encoding of CSV data. This is useful if you e.g. want to interleave I/O with parsing or if you want finer grained control over how you deal with type conversion errors.

Decoding example:

main :: IO ()
main = withFile "salaries.csv" ReadMode $ \ csvFile -> do
    let loop !_ (Fail _ errMsg) = putStrLn errMsg >> exitFailure
        loop acc (Many 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 NoHeader)
  where
    sumSalaries rs = sum [salary | Right (_ :: String, salary :: Int) <- rs]

Encoding example:

data Person = Person { name   :: !String, salary :: !Int }
    deriving Generic

instance FromNamedRecord Person
instance ToNamedRecord Person
instance DefaultOrdered Person

persons :: [Person]
persons = [Person "John" 50000, Person "Jane" 60000]

main :: IO ()
main = putStrLn $ encodeDefaultOrderedByName (go persons)
  where
    go (x:xs) = encodeNamedRecord x <> go xs
  • 5 types
  • 16 values
  • Packagecassava-0.5.3.2
  • Exports21
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceIncremental.hs

Decoding

4 declarations
datadata HeaderParser a
#

An incremental parser that when fed data eventually returns a parsed Header, or an error.

Constructors

  • FailH !ByteString String

    The input data was malformed. The first field contains any unconsumed input and second field contains information about the parse error.

  • PartialH (ByteString -> HeaderParser a)

    The parser needs more input data before it can produce a result. Use an empty string to indicate that no more input data is available. If fed an 'B.empty string', the continuation is guaranteed to return either FailH or DoneH.

  • DoneH !Header a

    The parse succeeded and produced the given Header.

Instances2Functor, Show

Just like in the case of non-incremental decoding, there are two ways to convert CSV records to and from and user-defined data types: index-based conversion and name-based conversion.

datadata Parser a
#

An incremental parser that when fed data eventually produces some parsed records, converted to the desired type, or an error in case of malformed input data.

Constructors

  • Fail !ByteString String

    The input data was malformed. The first field contains any unconsumed input and second field contains information about the parse error.

  • Many [Either String a] (ByteString -> Parser a)

    The parser parsed and converted zero or more records. Any records that failed type conversion are returned as Left errMsg and the rest as Right val. Feed a ByteString to the continuation to continue parsing. Use an empty string to indicate that no more input data is available. If fed an empty string, the continuation is guaranteed to return either Fail or Done.

  • Done [Either String a]

    The parser parsed and converted some records. Any records that failed type conversion are returned as Left errMsg and the rest as Right val.

Instances2Functor, Show
  • Functor ParserDefined in cassava-0.5.3.2 · Data.Csv.Incremental
  • Show a => Show (Parser a)Defined in cassava-0.5.3.2 · Data.Csv.Incremental

Index-based record conversion

See documentation on index-based conversion in Data.Csv for more information.

datadata HasHeader
#

Is the CSV data preceded by a header?

Constructors

  • HasHeader

    The CSV data is preceded by a header

  • NoHeader

    The CSV data is not preceded by a header

Name-based record conversion

See documentation on name-based conversion in Data.Csv for more information.

Encoding

0 declarations

Index-based record conversion

See documentation on index-based conversion in Data.Csv for more information.

newtypenewtype Builder a
#

A builder for building the CSV data incrementally. Just like the ByteString builder, this builder should be used in a right-associative, foldr style. Using <> to compose builders in a left-associative, foldl' style makes the building not be incremental.

Instances2Semigroup, Monoid

Name-based record conversion

See documentation on name-based conversion in Data.Csv for more information.

newtypenewtype NamedBuilder a
#

A builder for building the CSV data incrementally. Just like the ByteString builder, this builder should be used in a right-associative, foldr style. Using <> to compose builders in a left-associative, foldl' style makes the building not be incremental.

Instances2Semigroup, Monoid