[Haskell-cafe] Data.Binary, strict reading

Gwern Branwen gwern0 at gmail.com
Wed Feb 25 12:59:38 EST 2009


On Wed, Feb 25, 2009 at 12:15 PM, Neil Mitchell <ndmitchell at gmail.com> wrote:
> Hi,
>
> I want to read a file using Data.Binary, and I want to read the file
> strictly - i.e. when I leave the read file I want to guarantee the
> handle is closed. The reason is that (possibly immediately after) I
> need to write to the file. The following is the magic I need to use -
> is it all necessary, is it guaranteed correct, should I use something
> else?
>
>        src <- decodeFile "_make/_make"
>        Map.size mp `seq` performGC
>
> Thanks for all the helpful replies to the last post, I think I've
> nearly come up with a minimal test case I can give you, or at least
> characterise where the issue might lie. But I'll post to that thread
> when I've got the details.
>
> Thanks
>
> Neil

Funnily enough, I was just grappling with this issue in Yi. My
solution was to make the read strict, so my function looked like this:

readDB ∷  YiM ArticleDB
readDB = io $ (dbLocation >>= r) `catch` (λ_ →  return empty)
        where r x = fmap (decode · BL.fromChunks · return) $ B.readFile x

-- 
gwern


More information about the Haskell-Cafe mailing list