<div class="gmail_quote"><div>Hi Arnaud,</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">From: Arnaud Bailly &lt;<a href="mailto:arnaud.oqube@gmail.com" target="_blank">arnaud.oqube@gmail.com</a>&gt;<br>


<br>
Hello,<br>
I am trying to wrap my head around the concept of Iteratee reading the<br>
article by John Lato in Monad Reader issue 16<br>
(<a href="http://themonadreader.files.wordpress.com/2010/05/issue16.pdf" target="_blank">http://themonadreader.files.wordpress.com/2010/05/issue16.pdf</a>). I<br>
followed the &quot;advice&quot; on page 34:<br>
<br>
&quot;I have frequently heard reports from Haskellers (including highly-talented and<br>
experienced users) that the only way they could understand enumeration-based<br>
I/O was by re-implementing it themselves.&quot;<br>
<br>
and so reimplemented this code in Java which is, for better and worse,<br>
my current professional language (and there really is no point in<br>
writing my own 50th iteratee code...).<br>
<br>
I have a question about iteratees composition and their behaviour. I<br>
tried to use the throbber code to display progress while writing a<br>
file, without using enumeratees which are introduced later on in the<br>
article, but failed. I can bind the two iteratees together, but the<br>
writer iteratee consumes the stream of characters to write before<br>
handing over the control to the throbber iteratee which has nothing to<br>
count. Did I miss something ?<br></blockquote><div><br></div><div>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 &quot;streamToFile&quot; and &quot;throbber&quot; both consume an entire stream, they won&#39;t complete until EOF, and then will pass just that EOF to the next iteratee in the composition.</div>

<div><br></div><div>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&#39;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)</div>

<div><br></div><div>iterToEnum :: Monad m =&gt; Iteratee el m () -&gt; EnumerateeM el el m a</div><div><br></div><div>which would do this for any data-sink type iteratee.</div><div><br></div><div>A very similar approach is used in the &quot;iteratee&quot; package to define the &quot;enumPair&quot; function,</div>

<div><br></div><div>enumPair :: Monad m =&gt; Iteratee s m a -&gt; Iteratee s m b -&gt; Iteratee s m (a,b)</div><div><br></div><div>I consider &quot;enumPair&quot; to be one of the most powerful functions supplied in iteratee.</div>

<div><br></div><div>Since you use Java, you may be interested in an iteratee implementation in Scala, <a href="http://apocalisp.wordpress.com/2010/10/17/scalaz-tutorial-enumeration-based-io-with-iteratees/">http://apocalisp.wordpress.com/2010/10/17/scalaz-tutorial-enumeration-based-io-with-iteratees/</a></div>
<div><br></div><div>Best,</div><div>John</div></div>