Created
October 27, 2015 23:00
-
-
Save dimitri-xyz/f1f5bd4c0f7f2bf85379 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
import System.IO | |
import Control.Monad | |
import Control.Monad.IO.Class | |
import Control.Exception.Base (evaluate) | |
import Pipes | |
import Pipes.Prelude (stdoutLn) | |
withCSV :: FilePath -> (Handle -> IO r) -> IO r | |
withCSV path action = do | |
putStrLn "opening file" | |
h <- openFile path ReadMode | |
r <- action h | |
hClose h | |
putStrLn "file closed" | |
return r | |
getFileContents :: Producer String IO () | |
getFileContents = undefined -- do I go about writing this? | |
----------------------------------- | |
withCSVLifted :: MonadIO mIO => FilePath -> (Handle -> mIO r) -> mIO r | |
withCSVLifted path action = do | |
liftIO $ putStrLn "opening file" | |
h <- liftIO $ openFile path ReadMode | |
r <- action h | |
liftIO $ hClose h | |
liftIO $ putStrLn "file closed" | |
return r | |
getFileContentsLifted :: Producer String IO () | |
getFileContentsLifted = withCSVLifted "data.csv" myReadFile | |
where | |
myReadFile :: Handle -> Producer String IO () | |
myReadFile handle = do | |
eof <- lift $ hIsEOF handle | |
unless eof $ do | |
str <- lift $ hGetLine handle | |
yield str | |
myReadFile handle | |
lineDoubler :: Pipe String String IO () | |
lineDoubler = forever $ do | |
s <- await | |
yield s | |
yield s | |
main = do | |
runEffect $ getFileContentsLifted >-> lineDoubler >-> stdoutLn | |
----------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment