Skip to content

Instantly share code, notes, and snippets.

@dimitri-xyz
Created October 27, 2015 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimitri-xyz/f1f5bd4c0f7f2bf85379 to your computer and use it in GitHub Desktop.
Save dimitri-xyz/f1f5bd4c0f7f2bf85379 to your computer and use it in GitHub Desktop.
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