[Haskell-cafe] Re: Left fold enumerator - a real pearl overlooked?
Duncan Coutts
duncan.coutts at worc.ox.ac.uk
Mon Mar 2 11:16:49 EST 2009
On Mon, 2009-03-02 at 12:50 +0000, Bayley, Alistair wrote:
> > From: haskell-cafe-bounces at haskell.org
> > [mailto:haskell-cafe-bounces at haskell.org] On Behalf Of Duncan Coutts
> >
> > This can still be done using withFile and hGetContents. You
> > just have to
> > put the consumer inside the scope of withFile. The consumer
> > can work in
> > a streaming fashion. With lazy bytestrings this can be both efficient,
> > work in constant memory and guarantee the file is closed.
> >
> > We guarantee the file is closed by using withFile. The only thing to
> > watch out for is a consumer that doesn't consume as much as you were
> > expecting before the file does get closed. You should notice
> > that pretty
> > quickly though since it should happen every time (whereas
> > resource leaks
> > are not so immediately visible).
>
> Sure. But this case is the one that typically causes problems for
> beginners, who have not yet had the educating experience of being
> bitten by lazy IO. The standard café response to "why do I get <handle
> closed>" errors here?" is "you're using hGetContents and you haven't
> forced/consumed all of your file".
This is quite true, but I expect that's easier to explain to beginners
than iterator IO, at least at the present time.
Duncan
More information about the Haskell-Cafe
mailing list