Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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