
On Sun, Jul 12, 2009 at 4:27 AM, Felipe Lessa
On Sat, Jul 11, 2009 at 08:40:10PM -0400, Keith Sheppard wrote:
4) Is there any other wacky stuff in my code that I should change?
I would probably write readBinFiles as
readBinFiles :: [String] -> IO [BS.ByteString] readBinFiles = mapM readB where readB file = openBinaryFile file ReadMode >>= BS.hGetContents
You may also write pointless code ;)
readBinFiles :: [String] -> IO [BS.ByteString] readBinFiles = mapM_ $ flip (>>=) BS.hGetContents . flip openBinaryFile ReadMode
You can greatly improve that by using the kleisli composition operator :
readBinFiles = mapM (BS.hGetContents <=< flip openBinaryFile ReadMode)
But if this is lazy bytestrings, this will leak handles like crazy... A nice solution would be to use the safe-lazy-io package : it is easy to add a finalizer that will remove the file once hGetContents is finished (with System.IO.Lazy.Internal.finallyLI) and to read a list of files lazily without leaking handles (see System.IO.Lazy.concat). http://hackage.haskell.org/package/safe-lazy-io -- Jedaï