Last active
August 29, 2015 14:02
-
-
Save bgamari/efad8560ab7dd38e9407 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE TemplateHaskell #-} | |
import StaticCSV | |
import Data.Proxy | |
myData = $(staticCSV "my-data.csv" (Proxy :: Proxy (Int, String))) | |
main = print myData |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 | hi | |
---|---|---|
6 | hello | |
7 | hmm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE TemplateHaskell, FlexibleContexts #-} | |
module StaticCSV (staticCSV) where | |
import Control.Applicative | |
import Data.Csv as Csv hiding (Name) | |
import Data.Proxy | |
import Data.Data | |
import Language.Haskell.TH | |
import Language.Haskell.TH.Syntax (Lift, lift) | |
import qualified Data.ByteString.Lazy as BSL | |
import qualified Data.Vector as V | |
staticCSV :: (FromRecord a, Lift (V.Vector a)) => FilePath -> Proxy a -> ExpQ | |
staticCSV fileName ty = do | |
contents <- runIO $ BSL.readFile fileName | |
csv <- case decode NoHeader contents of | |
Right a -> return $ fmap (flip asProxyTypeOf ty) a | |
Left err -> fail err | |
lift csv | |
instance Lift a => Lift (V.Vector a) where | |
lift v = do | |
list <- ListE <$> mapM lift (V.toList v) | |
return $ AppE (VarE 'V.fromList) list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment