summaryrefslogtreecommitdiff
path: root/src/Data/CSV/Table/Types.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Data/CSV/Table/Types.hs')
-rw-r--r--src/Data/CSV/Table/Types.hs52
1 files changed, 31 insertions, 21 deletions
diff --git a/src/Data/CSV/Table/Types.hs b/src/Data/CSV/Table/Types.hs
index 1ab45e1..c81b170 100644
--- a/src/Data/CSV/Table/Types.hs
+++ b/src/Data/CSV/Table/Types.hs
@@ -4,19 +4,22 @@ module Data.CSV.Table.Types (
-- * Representation
Table (..)
, Row (..)
- , Col (..)
- , RowInfo
+ , Col (..)
+ , RowInfo
+ , TField (..)
+ , Order (..)
-- * Accessors
, getCols
, getRows
, lookupCol
- -- * Parsing
+ -- * Parsing
, fromFile
+ , fromString
-- * Saving
- , toFile
+ , toFile
) where
@@ -38,7 +41,7 @@ type RowInfo = [(Col, Field)]
data Table = T { dim :: Int, cols :: [Col], body :: [Row]}
-{-@ measure width :: Row -> Int
+{-@ measure width :: Row -> Int
width (R xs) = (len xs) @-}
{-@ type ColsN N = {v:[Col] | (len v) = N} @-}
@@ -54,35 +57,44 @@ getRows t = [r | R r <- body t]
lookupCol :: Col -> RowInfo -> Field
lookupCol c cxs = fromMaybe err $ lookup c cxs
where
- err = printf "lookupCol: cannot find %s in %s" (show c) (show cxs)
-
+ err = printf "lookupCol: cannot find %s in %s" (show c) (show cxs)
+
+--------------------------------------------------------------------------------
+-- | Field Sorts
+--------------------------------------------------------------------------------
+
+data TField = FStr | FInt | FDbl
+ deriving (Eq, Ord, Show)
+
+data Order = Asc | Dsc
+ deriving (Eq, Ord, Show)
----------------------------------------------------------------------
--- | Converting to CSV
+-- | Converting to CSV
----------------------------------------------------------------------
fromCSV :: CSV -> Table
fromCSV [] = error "fromCSV: Empty CSV with no rows!"
-fromCSV (r:rs) = T n cs b
+fromCSV (r:rs) = T n cs b
where
n = length r
cs = [C x | x <- r]
- b = mapMaybe (makeRow n) $ zip [0..] rs
+ b = mapMaybe (makeRow n) $ zip [0..] rs
makeRow :: Int -> (Int, Record) -> Maybe Row
-makeRow n (i, xs)
+makeRow n (i, xs)
| length xs == n = Just $ R xs
- | empty xs = Nothing
+ | empty xs = Nothing
| otherwise = error $ printf "Row %d does not have %d columns:\n%s" i n (show xs)
empty :: Record -> Bool
empty = null . unwords
toCSV :: Table -> CSV
-toCSV t = [c | C c <- cols t] : [xs | R xs <- body t]
+toCSV t = [c | C c <- cols t] : [xs | R xs <- body t]
--------------------------------------------------------------------
--- | Parsing
--------------------------------------------------------------------
+--------------------------------------------------------------------------------
+-- | Parsing
+--------------------------------------------------------------------------------
toFile :: FilePath -> Table -> IO ()
toFile f = writeFile f . show
@@ -93,15 +105,13 @@ fromFile f = fromString f <$> readFile f
fromString :: FilePath -> String -> Table
fromString fp s = fromCSV $ parseCSV' fp s
-parseCSV' fp s = case parseCSV fp s of
+parseCSV' fp s = case parseCSV fp s of
Right c -> c
Left e -> error $ printf "parseCSV': %s" (show e)
--------------------------------------------------------------------
+--------------------------------------------------------------------------------
-- | Printing
--------------------------------------------------------------------
+--------------------------------------------------------------------------------
instance Show Table where
show = printCSV . toCSV
-
-