subhask-0.1.0.0: Type safe interface for programming in subcategories of Hask

Safe HaskellNone
LanguageHaskell2010

SubHask.Compatibility.Cassava

Contents

Synopsis

Documentation

decode_ :: FromRecord a => HasHeader -> PartitionOnNewline (ByteString Char) -> Either String (BArray a) Source

This is a monoid homomorphism, which means it can be parallelized

decode :: (NFData a, FromRecord a, ValidEq a) => HasHeader -> ByteString Char -> Either String (BArray a) Source

Like the "decode" function in Data.Csv, but works in parallel

Types

class FromRecord a

A type that can be converted from a single CSV record, with the possibility of failure.

When writing an instance, use empty, mzero, or fail to make a conversion fail, e.g. if a Record has the wrong number of columns.

Given this example data:

John,56
Jane,55

here's an example type and instance:

data Person = Person { name :: !Text, age :: !Int }

instance FromRecord Person where
    parseRecord v
        | length v == 2 = Person <$>
                          v .! 0 <*>
                          v .! 1
        | otherwise     = mzero

Instances

FromField a => FromRecord [a] 
FromField a => FromRecord (Only a) 
FromField a => FromRecord (Vector a) 
(FromField a, Unbox a) => FromRecord (Vector a) 
(FromField a, FromField b) => FromRecord (a, b) 
(FromField a, FromField b, FromField c) => FromRecord (a, b, c) 
(FromField r, ValidSVector Symbol n r, IsScalar r, FreeModule r) => FromRecord (SVector Symbol n r) 
(FromField r, Prim r, IsScalar r, FreeModule r) => FromRecord (UVector Symbol n r) 
(FromField a, FromField b, FromField c, FromField d) => FromRecord (a, b, c, d) 
(FromField a, FromField b, FromField c, FromField d, FromField e) => FromRecord (a, b, c, d, e) 
(FromField a, FromField b, FromField c, FromField d, FromField e, FromField f) => FromRecord (a, b, c, d, e, f) 
(FromField a, FromField b, FromField c, FromField d, FromField e, FromField f, FromField g) => FromRecord (a, b, c, d, e, f, g) 
(FromField a, FromField b, FromField c, FromField d, FromField e, FromField f, FromField g, FromField h) => FromRecord (a, b, c, d, e, f, g, h) 
(FromField a, FromField b, FromField c, FromField d, FromField e, FromField f, FromField g, FromField h, FromField i) => FromRecord (a, b, c, d, e, f, g, h, i) 

class ToRecord a

A type that can be converted to a single CSV record.

An example type and instance:

data Person = Person { name :: !Text, age :: !Int }

instance ToRecord Person where
    toRecord (Person name age) = record [
        toField name, toField age]

Outputs data on this form:

John,56
Jane,55

Instances

ToField a => ToRecord [a] 
ToField a => ToRecord (Only a) 
ToField a => ToRecord (Vector a) 
(ToField a, Unbox a) => ToRecord (Vector a) 
(ToField a, ToField b) => ToRecord (a, b) 
(ToField a, ToField b, ToField c) => ToRecord (a, b, c) 
(ToField a, ToField b, ToField c, ToField d) => ToRecord (a, b, c, d) 
(ToField a, ToField b, ToField c, ToField d, ToField e) => ToRecord (a, b, c, d, e) 
(ToField a, ToField b, ToField c, ToField d, ToField e, ToField f) => ToRecord (a, b, c, d, e, f) 
(ToField a, ToField b, ToField c, ToField d, ToField e, ToField f, ToField g) => ToRecord (a, b, c, d, e, f, g) 
(ToField a, ToField b, ToField c, ToField d, ToField e, ToField f, ToField g, ToField h) => ToRecord (a, b, c, d, e, f, g, h) 
(ToField a, ToField b, ToField c, ToField d, ToField e, ToField f, ToField g, ToField h, ToField i) => ToRecord (a, b, c, d, e, f, g, h, i) 

class FromField a

A type that can be converted from a single CSV field, with the possibility of failure.

When writing an instance, use empty, mzero, or fail to make a conversion fail, e.g. if a Field can't be converted to the given type.

Example type and instance:

{-# LANGUAGE OverloadedStrings #-}

data Color = Red | Green | Blue

instance FromField Color where
    parseField s
        | s == "R"  = pure Red
        | s == "G"  = pure Green
        | s == "B"  = pure Blue
        | otherwise = mzero

Minimal complete definition

parseField

Instances

FromField Char

Assumes UTF-8 encoding.

FromField Double

Accepts same syntax as rational. Ignores whitespace.

FromField Float

Accepts same syntax as rational. Ignores whitespace.

FromField Int

Accepts a signed decimal number. Ignores whitespace.

FromField Int8

Accepts a signed decimal number. Ignores whitespace.

FromField Int16

Accepts a signed decimal number. Ignores whitespace.

FromField Int32

Accepts a signed decimal number. Ignores whitespace.

FromField Int64

Accepts a signed decimal number. Ignores whitespace.

FromField Integer

Accepts a signed decimal number. Ignores whitespace.

FromField Word

Accepts an unsigned decimal number. Ignores whitespace.

FromField Word8

Accepts an unsigned decimal number. Ignores whitespace.

FromField Word16

Accepts an unsigned decimal number. Ignores whitespace.

FromField Word32

Accepts an unsigned decimal number. Ignores whitespace.

FromField Word64

Accepts an unsigned decimal number. Ignores whitespace.

FromField ()

Ignores the Field. Always succeeds.

FromField ByteString 
FromField ByteString 
FromField Text

Assumes UTF-8 encoding. Fails on invalid byte sequences.

FromField Text

Assumes UTF-8 encoding. Fails on invalid byte sequences.

FromField [Char]

Assumes UTF-8 encoding. Fails on invalid byte sequences.

FromField a => FromField (Maybe a)

Nothing if the Field is empty, Just otherwise.

FromField a => FromField (Either Field a)

Left field if conversion failed, Right otherwise.

class ToField a

A type that can be converted to a single CSV field.

Example type and instance:

{-# LANGUAGE OverloadedStrings #-}

data Color = Red | Green | Blue

instance ToField Color where
    toField Red   = "R"
    toField Green = "G"
    toField Blue  = "B"

Minimal complete definition

toField

Instances

ToField Char

Uses UTF-8 encoding.

ToField Double

Uses decimal notation or scientific notation, depending on the number.

ToField Float

Uses decimal notation or scientific notation, depending on the number.

ToField Int

Uses decimal encoding with optional sign.

ToField Int8

Uses decimal encoding with optional sign.

ToField Int16

Uses decimal encoding with optional sign.

ToField Int32

Uses decimal encoding with optional sign.

ToField Int64

Uses decimal encoding with optional sign.

ToField Integer

Uses decimal encoding with optional sign.

ToField Word

Uses decimal encoding.

ToField Word8

Uses decimal encoding.

ToField Word16

Uses decimal encoding.

ToField Word32

Uses decimal encoding.

ToField Word64

Uses decimal encoding.

ToField ByteString 
ToField ByteString 
ToField Text

Uses UTF-8 encoding.

ToField Text

Uses UTF-8 encoding.

ToField [Char]

Uses UTF-8 encoding.

ToField a => ToField (Maybe a)

Nothing is encoded as an empty field.

data 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