Created
May 4, 2014 21:13
-
-
Save Davorak/f75f6df327cbc4742f16 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
{- | File "tmp2" fails to close in this example | |
- ghci> runSafeT . runEffect $ readFile' "tmp" >-> writeFile' "tmp2" | |
- {tmp2 open} | |
- {tmp open} | |
- {tmp closed} | |
-} | |
{-# language RankNTypes #-} | |
{-# language NoMonomorphismRestriction #-} | |
import Pipes as Pipes | |
import qualified Pipes.Prelude as P | |
import Pipes.Safe | |
import qualified Pipes.Safe.Prelude as PSP | |
import qualified System.IO as IO | |
import Prelude hiding (readFile) | |
import Control.Monad | |
readFile :: (MonadSafe m) => FilePath -> Producer' String m () | |
readFile file = bracket | |
(do h <- liftIO $ IO.openFile file IO.ReadMode | |
liftIO $ putStrLn $ "{" ++ file ++ " open}" | |
return h ) | |
(\h -> do | |
liftIO $ IO.hClose h | |
liftIO $ putStrLn $ "{" ++ file ++ " closed}" ) | |
P.fromHandle | |
writeFile :: (MonadSafe m) => FilePath -> Consumer' String m () | |
writeFile file = bracket | |
(do h <- liftIO $ IO.openFile file IO.WriteMode | |
liftIO $ putStrLn $ "{" ++ file ++ " open}" | |
return h ) | |
(\h -> do | |
liftIO $ IO.hClose h | |
liftIO $ putStrLn $ "{" ++ file ++ " closed}" ) | |
P.toHandle | |
readFile' fileName = runEffect | |
. runSafeP $ readFile fileName | |
>-> forever (await >>= lift . lift . yield) | |
writeFile' fileName = runEffect | |
. runSafeP | |
$ forever ((lift . lift) await >>= yield) | |
>-> Main.writeFile fileName |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment