[Haskell-beginners] external sort
Chaddaï Fouché
chaddai.fouche at gmail.com
Sun Jul 12 05:04:41 EDT 2009
On Sun, Jul 12, 2009 at 4:27 AM, Felipe Lessa<felipe.lessa at gmail.com> wrote:
> 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ï
More information about the Beginners
mailing list