Skip to content

Instantly share code, notes, and snippets.

@berdario
Created January 16, 2015 19:28
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 berdario/32d055273b74ae6ed76f to your computer and use it in GitHub Desktop.
Save berdario/32d055273b74ae6ed76f to your computer and use it in GitHub Desktop.
I felt the need for an n-ary version of withFile. It works, but for something this simple, readFile + writeFile would've been enough
import System.Environment (getArgs)
import System.IO (withFile, IOMode(..), FilePath, Handle, hGetContents, hPutStr)
import Data.Functor ((<$>))
withFiles' :: [Handle] -> [(IOMode, FilePath)] -> ([Handle] -> IO r) -> IO r
withFiles' handles [] f = f $ reverse handles
withFiles' handles ((mode, fpath):xs) f = withFile fpath mode (\x -> withFiles' (x:handles) xs f)
withFiles = withFiles' []
cat [infile, outfile] = do
text <- hGetContents infile
hPutStr outfile text
main = do
inputs <- zip [ReadMode, WriteMode] <$> getArgs
withFiles inputs cat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment