[Haskell-cafe] Re: Haskell-Cafe Digest, Vol 86, Issue 63

Arnaud Bailly arnaud.oqube at gmail.com
Sat Oct 30 06:52:09 EDT 2010


Hello John,
Thanks for your detailed answer. I finally understood this behaviour
and got to the point where I need something like enumPair.
About the scala implementation, it was this same blog post that arose
my interest in iteratees :-)

Regards,
Arnaud
On Wed, Oct 27, 2010 at 5:26 PM, John Lato <jwlato at gmail.com> wrote:
> Hi Arnaud,
>
>>
>> From: Arnaud Bailly <arnaud.oqube at gmail.com>
>>
>> Hello,
>> 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 and
>> experienced users) that the only way they could understand
>> enumeration-based
>> 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" function,
> enumPair :: Monad m => Iteratee s m a -> Iteratee s m b -> Iteratee s m
> (a,b)
> I consider "enumPair" to be one of the most powerful functions supplied in
> iteratee.
> Since you use Java, you may be interested in an iteratee implementation in
> Scala, http://apocalisp.wordpress.com/2010/10/17/scalaz-tutorial-enumeration-based-io-with-iteratees/
> Best,
> John


More information about the Haskell-Cafe mailing list