[Haskell-cafe] Re: Haskell-Cafe Digest, Vol 86, Issue 63
jwlato at gmail.com
Wed Oct 27 11:26:58 EDT 2010
> From: Arnaud Bailly <arnaud.oqube at gmail.com>
> I am trying to wrap my head around the concept of Iteratee reading the
> article by John Lato in Monad Reader issue 16
> (http://themonadreader.files.wordpress.com/2010/05/issue16.pdf). I
> followed the "advice" on page 34:
> "I have frequently heard reports from Haskellers (including highly-talented
> experienced users) that the only way they could understand
> I/O was by re-implementing it themselves."
> and so reimplemented this code in Java which is, for better and worse,
> my current professional language (and there really is no point in
> writing my own 50th iteratee code...).
> I have a question about iteratees composition and their behaviour. I
> tried to use the throbber code to display progress while writing a
> file, without using enumeratees which are introduced later on in the
> article, but failed. I can bind the two iteratees together, but the
> writer iteratee consumes the stream of characters to write before
> handing over the control to the throbber iteratee which has nothing to
> count. Did I miss something ?
This is the expected behavior. When iteratees are composed with bind, the
first will consume as much of the stream as it needs to calculate its
result, and only then will it pass the remaining stream to the next iteratee
in the composition. Since "streamToFile" and "throbber" both consume an
entire stream, they won't complete until EOF, and then will pass just that
EOF to the next iteratee in the composition.
The behavior you want is pretty common, and it can be accomplished by
writing either throbber or streamToFile as an enumeratee. Then the
enumeratee version would do the work it's supposed to but also pass the
stream along to another iteratee. You could write a library function with
type (following the types from the article)
iterToEnum :: Monad m => Iteratee el m () -> EnumerateeM el el m a
which would do this for any data-sink type iteratee.
A very similar approach is used in the "iteratee" package to define the
enumPair :: Monad m => Iteratee s m a -> Iteratee s m b -> Iteratee s m
I consider "enumPair" to be one of the most powerful functions supplied in
Since you use Java, you may be interested in an iteratee implementation in
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe