module System.Console.Hawk.Representable (
ListAsRow (listRepr')
, ListAsRows (listRepr)
, Row (repr')
, Rows (repr)
) where
import Prelude
import Data.ByteString.Lazy.Char8 (ByteString)
import qualified Data.ByteString.Lazy.Char8 as C8 hiding (hPutStrLn)
import qualified Data.List as L
import Data.Set (Set)
import qualified Data.Set as S
import Data.Map (Map)
import qualified Data.Map as M
class (Show a) => ListAsRow a where
listRepr' :: ByteString -> [a] -> ByteString
listRepr' d = C8.intercalate d . L.map (C8.pack . show)
instance ListAsRow Bool
instance ListAsRow Float
instance ListAsRow Double
instance ListAsRow Int
instance ListAsRow Integer
instance ListAsRow ()
instance (ListAsRow a) => ListAsRow [a] where
listRepr' d = C8.intercalate d . L.map (listRepr' d)
instance (Row a) => ListAsRow (Maybe a) where
listRepr' d = C8.intercalate d . L.map (repr' d)
instance (ListAsRow a) => ListAsRow (Set a) where
listRepr' d = listRepr' d . L.map (listRepr' d . S.toList)
instance ListAsRow Char where
listRepr' _ = C8.pack
instance ListAsRow ByteString where
listRepr' d = C8.intercalate d
instance (Row a, Row b) => ListAsRow (Map a b) where
listRepr' d = listRepr' d . L.map (listRepr' d . M.toList)
instance (Row a,Row b) => ListAsRow (a,b) where
listRepr' d = C8.intercalate d . L.map (\(x,y) -> C8.unwords
[repr' d x,repr' d y])
instance (Row a,Row b,Row c) => ListAsRow (a,b,c) where
listRepr' d = C8.intercalate d . L.map (\(x,y,z) -> C8.unwords
[repr' d x,repr' d y,repr' d z])
instance (Row a,Row b,Row c,Row d) => ListAsRow (a,b,c,d) where
listRepr' d = C8.intercalate d . L.map (\(a,b,c,e) -> C8.unwords
[repr' d a,repr' d b,repr' d c,repr' d e])
instance (Row a,Row b,Row c,Row d,Row e) => ListAsRow (a,b,c,d,e) where
listRepr' d = C8.intercalate d . L.map (\(a,b,c,e,f) -> C8.unwords
[repr' d a,repr' d b,repr' d c,repr' d e,repr' d f])
instance (Row a,Row b,Row c,Row d,Row e,Row f) => ListAsRow (a,b,c,d,e,f) where
listRepr' d = C8.intercalate d . L.map (\(a,b,c,e,f,g) -> C8.unwords
[repr' d a,repr' d b,repr' d c,repr' d e,repr' d f
,repr' d g])
instance (Row a,Row b,Row c,Row d,Row e,Row f,Row g)
=> ListAsRow (a,b,c,d,e,f,g) where
listRepr' d = C8.intercalate d . L.map (\(a,b,c,e,f,g,h) -> C8.unwords
[repr' d a,repr' d b,repr' d c,repr' d e,repr' d f
,repr' d g,repr' d h])
instance (Row a,Row b,Row c,Row d,Row e,Row f,Row g,Row h)
=> ListAsRow (a,b,c,d,e,f,g,h) where
listRepr' d = C8.intercalate d . L.map (\(a,b,c,e,f,g,h,i) -> C8.unwords
[repr' d a,repr' d b,repr' d c,repr' d e,repr' d f
,repr' d g,repr' d h,repr' d i])
instance (Row a,Row b,Row c,Row d,Row e,Row f,Row g,Row h,Row i)
=> ListAsRow (a,b,c,d,e,f,g,h,i) where
listRepr' d = C8.intercalate d . L.map (\(a,b,c,e,f,g,h,i,l) -> C8.unwords
[repr' d a,repr' d b,repr' d c,repr' d e,repr' d f
,repr' d g,repr' d h,repr' d i,repr' d l])
instance (Row a,Row b,Row c,Row d,Row e,Row f,Row g,Row h,Row i,Row l)
=> ListAsRow (a,b,c,d,e,f,g,h,i,l) where
listRepr' d = C8.intercalate d . L.map (\(a,b,c,e,f,g,h,i,l,m) -> C8.unwords
[repr' d a,repr' d b,repr' d c,repr' d e,repr' d f
,repr' d g,repr' d h,repr' d i,repr' d l,repr' d m])
class (Show a) => Row a where
repr' :: ByteString
-> a
-> ByteString
repr' _ = C8.pack . show
instance Row Bool
instance Row Float
instance Row Double
instance Row Int
instance Row Integer
instance Row ()
instance Row Char where
repr' _ = C8.singleton
instance (ListAsRow a) => Row [a] where
repr' = listRepr'
instance (ListAsRow a) => Row (Set a) where
repr' d = listRepr' d . S.toList
instance (Row a,Row b) => Row (Map a b) where
repr' d = listRepr' d . M.toList
instance Row ByteString where
repr' _ = id
instance (Row a) => Row (Maybe a) where
repr' _ Nothing = C8.empty
repr' d (Just x) = repr' d x
instance (Row a,Row b) => Row (a,b) where
repr' d (a,b) = repr' d a `C8.append` (d `C8.append` repr' d b)
instance (Row a,Row b,Row c) => Row (a,b,c) where
repr' d (a,b,c) = repr' d a `C8.append` (d `C8.append`
(repr' d b `C8.append` (d `C8.append` repr' d c)))
instance (Row a,Row b,Row c,Row d) => Row (a,b,c,d) where
repr' d (a,b,c,e) = repr' d a `C8.append` (d `C8.append`
(repr' d b `C8.append` (d `C8.append`
(repr' d c `C8.append` (d `C8.append` repr' d e)))))
instance (Row a,Row b,Row c,Row d,Row e) => Row (a,b,c,d,e) where
repr' d (a,b,c,e,f) = repr' d a `C8.append` (d `C8.append`
(repr' d b `C8.append` (d `C8.append`
(repr' d c `C8.append` (d `C8.append`
(repr' d e `C8.append` (d `C8.append` repr' d f)))))))
instance (Row a,Row b,Row c,Row d,Row e,Row f) => Row (a,b,c,d,e,f) where
repr' d (a,b,c,e,f,g) = repr' d a `C8.append` (d `C8.append`
(repr' d b `C8.append` (d `C8.append`
(repr' d c `C8.append` (d `C8.append`
(repr' d e `C8.append` (d `C8.append`
(repr' d f `C8.append` (d `C8.append` repr' d g)))))))))
instance (Row a,Row b,Row c,Row d,Row e,Row f,Row g) => Row (a,b,c,d,e,f,g) where
repr' d (a,b,c,e,f,g,h) = repr' d a `C8.append` (d `C8.append`
(repr' d b `C8.append` (d `C8.append`
(repr' d c `C8.append` (d `C8.append`
(repr' d e `C8.append` (d `C8.append`
(repr' d f `C8.append` (d `C8.append`
(repr' d g `C8.append` (d `C8.append` repr' d h)))))))))))
instance (Row a,Row b,Row c,Row d,Row e,Row f,Row g,Row h)
=> Row (a,b,c,d,e,f,g,h) where
repr' d (a,b,c,e,f,g,h,i) =
repr' d a `C8.append` (d `C8.append`
(repr' d b `C8.append` (d `C8.append`
(repr' d c `C8.append` (d `C8.append`
(repr' d e `C8.append` (d `C8.append`
(repr' d f `C8.append` (d `C8.append`
(repr' d g `C8.append` (d `C8.append`
(repr' d h `C8.append` (d `C8.append` repr' d i)))))))))))))
instance (Row a,Row b,Row c,Row d,Row e,Row f,Row g,Row h,Row i)
=> Row (a,b,c,d,e,f,g,h,i) where
repr' d (a,b,c,e,f,g,h,i,l) =
repr' d a `C8.append` (d `C8.append`
(repr' d b `C8.append` (d `C8.append`
(repr' d c `C8.append` (d `C8.append`
(repr' d e `C8.append` (d `C8.append`
(repr' d f `C8.append` (d `C8.append`
(repr' d g `C8.append` (d `C8.append`
(repr' d h `C8.append` (d `C8.append`
(repr' d i `C8.append` (d `C8.append` repr' d l)))))))))))))))
instance (Row a,Row b,Row c,Row d,Row e,Row f,Row g,Row h,Row i,Row l)
=> Row (a,b,c,d,e,f,g,h,i,l) where
repr' d (a,b,c,e,f,g,h,i,l,m) =
repr' d a `C8.append` (d `C8.append`
(repr' d b `C8.append` (d `C8.append`
(repr' d c `C8.append` (d `C8.append`
(repr' d e `C8.append` (d `C8.append`
(repr' d f `C8.append` (d `C8.append`
(repr' d g `C8.append` (d `C8.append`
(repr' d h `C8.append` (d `C8.append`
(repr' d i `C8.append` (d `C8.append`
(repr' d l `C8.append` (d `C8.append` repr' d m)))))))))))))))))
class (Row a) => ListAsRows a where
listRepr :: ByteString
-> [a]
-> [ByteString]
listRepr d = L.map (repr' d)
instance ListAsRows ByteString
instance ListAsRows Bool
instance ListAsRows Double
instance ListAsRows Float
instance ListAsRows Int
instance ListAsRows Integer
instance (Row a) => ListAsRows (Maybe a)
instance ListAsRows ()
instance (ListAsRow a,ListAsRows a) => ListAsRows [a]
instance (Row a,Row b) => ListAsRows (a,b)
instance (Row a,Row b,Row c) => ListAsRows (a,b,c)
instance (Row a,Row b,Row c,Row d) => ListAsRows (a,b,c,d)
instance (Row a,Row b,Row c,Row d,Row e) => ListAsRows (a,b,c,d,e)
instance (Row a,Row b,Row c,Row d,Row e,Row f) => ListAsRows (a,b,c,d,e,f)
instance (Row a,Row b,Row c,Row d,Row e,Row f,Row g) => ListAsRows (a,b,c,d,e,f,g)
instance (Row a,Row b,Row c,Row d,Row e,Row f,Row g,Row h)
=> ListAsRows (a,b,c,d,e,f,g,h)
instance (Row a,Row b,Row c,Row d,Row e,Row f,Row g,Row h,Row i)
=> ListAsRows (a,b,c,d,e,f,g,h,i)
instance (Row a,Row b,Row c,Row d,Row e,Row f,Row g,Row h,Row i,Row l)
=> ListAsRows (a,b,c,d,e,f,g,h,i,l)
instance ListAsRows Char where
listRepr _ = (:[]) . C8.pack
instance (ListAsRow a,ListAsRows a) => ListAsRows (Set a) where
listRepr d = listRepr d . L.map S.toList
instance (Row a,Row b) => ListAsRows (Map a b) where
listRepr d = listRepr d . L.map M.toList
instance (ListAsRows a) => Rows [a] where
repr = listRepr
class (Show a) => Rows a where
repr :: ByteString
-> a
-> [C8.ByteString]
repr _ = (:[]) . C8.pack . show
instance Rows Bool
instance Rows Double
instance Rows Float
instance Rows Int
instance Rows Integer
instance Rows () where
repr _ = const [C8.empty]
instance Rows Char where
repr _ = (:[]) . C8.singleton
instance Rows ByteString where
repr _ = (:[])
instance (Rows a) => Rows (Maybe a) where
repr d = maybe [C8.empty] (repr d)
instance (Row a, Row b) => Rows (Map a b) where
repr d = listRepr d . M.toList
instance (ListAsRows a) => Rows (Set a) where
repr d = listRepr d . S.toList
instance (Row a, Row b) => Rows (a,b) where
repr d (x,y) = [repr' d x,repr' d y]
instance (Row a, Row b, Row c) => Rows (a,b,c) where
repr d (a,b,c) = [repr' d a, repr' d b, repr' d c]
instance (Row a, Row b, Row c, Row d) => Rows (a,b,c,d) where
repr d (a,b,c,e) = [repr' d a, repr' d b, repr' d c, repr' d e]
instance (Row a, Row b, Row c, Row d, Row e) => Rows (a,b,c,d,e) where
repr d (a,b,c,e,f) = [repr' d a, repr' d b, repr' d c, repr' d e, repr' d f]
instance (Row a, Row b, Row c, Row d, Row e, Row f) => Rows (a,b,c,d,e,f) where
repr d (a,b,c,e,f,g) = [repr' d a, repr' d b, repr' d c,repr' d e
,repr' d f, repr' d g]
instance (Row a, Row b, Row c, Row d, Row e, Row f, Row g)
=> Rows (a,b,c,d,e,f,g) where
repr d (a,b,c,e,f,g,h) = [repr' d a, repr' d b, repr' d c,repr' d e
,repr' d f, repr' d g, repr' d h]
instance (Row a, Row b, Row c, Row d, Row e, Row f, Row g, Row h)
=> Rows (a,b,c,d,e,f,g,h) where
repr d (a,b,c,e,f,g,h,i) = [repr' d a, repr' d b, repr' d c, repr' d e
,repr' d f, repr' d g, repr' d h, repr' d i]
instance (Row a, Row b, Row c, Row d, Row e, Row f, Row g, Row h, Row i)
=> Rows (a,b,c,d,e,f,g,h,i) where
repr d (a,b,c,e,f,g,h,i,l) = [repr' d a, repr' d b, repr' d c, repr' d e
,repr' d f, repr' d g, repr' d h, repr' d i
, repr' d l]
instance (Row a, Row b, Row c, Row d, Row e, Row f, Row g, Row h, Row i, Row l)
=> Rows (a,b,c,d,e,f,g,h,i,l) where
repr d (a,b,c,e,f,g,h,i,l,m) = [repr' d a, repr' d b, repr' d c, repr' d e
,repr' d f, repr' d g, repr' d h, repr' d i
,repr' d l, repr' d m]