Created
January 16, 2015 19:28
-
-
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
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
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