Skip to content

Instantly share code, notes, and snippets.

Created March 17, 2013 19:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5183107 to your computer and use it in GitHub Desktop.
Save anonymous/5183107 to your computer and use it in GitHub Desktop.
import Control.Monad
import Control.Monad.Trans.Class
import Control.Monad.IO.Class
import Data.Conduit
import Data.Conduit.List
import System.Environment
import System.IO
{- | Accept file paths on input, output opened file handle, and ensure that the
- handle is always closed after its downstream pipe finishes whatever work on it. -}
fileConduit :: MonadResource m => IOMode -> Conduit FilePath m Handle
fileConduit mode = awaitForever process
where
process file = bracketP (openFile file mode) closeWithMsg yield
closeWithMsg h = do
putStrLn "Closing file"
hClose h
{- | Print the first line from each handle on input. -}
firstLine :: MonadIO m => Sink Handle m ()
firstLine = awaitForever (liftIO . (hGetLine >=> putStrLn))
main = do
args <- getArgs
runResourceT $ sourceList args =$= fileConduit ReadMode $$ firstLine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment