<div dir="ltr">Yes, that&#39;s a fair explanation.</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Apr 9, 2013 at 7:48 AM, Magicloud Magiclouds <span dir="ltr">&lt;<a href="mailto:magicloud.magiclouds@gmail.com" target="_blank">magicloud.magiclouds@gmail.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Thank you for the reply. I&#39;ve learnt the code of &quot;lines&quot;. So it is because how ByteString works, that the conduit is not a stream of bytes, but chunks, right?</div>

</div><div class="HOEnZb"><div class="h5"><div class="gmail_extra">
<br><br><div class="gmail_quote">On Tue, Apr 9, 2013 at 12:12 PM, Michael Snoyman <span dir="ltr">&lt;<a href="mailto:michael@snoyman.com" target="_blank">michael@snoyman.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<div dir="ltr">It&#39;s a bug in your implementation of takeLine I believe. It doesn&#39;t take into account that lines can span multiple chunks. When you call takeLine the first time, you get &quot;L1\n&quot;. leftover puts a chunk with exactly those contents back. When you call takeLine the second time, it gets the chunk &quot;L1\n&quot;, and your splitAt gives you back &quot;L1\n&quot; and &quot;&quot;. The &quot;&quot; is then leftover, and the next call to takeLine gets it.<div>




<br></div><div>Your takeLine needs to include logic saying &quot;there&#39;s no newline in this chunk at all, let&#39;s get the next chunk and try that.&quot; You can look at the source to lines[1] for an example of the concept.</div>




<div><br></div><div>Michael</div><div><br></div><div>[1]&nbsp;<a href="http://haddocks.fpcomplete.com/fp/7.4.2/20130313-1/conduit/src/Data-Conduit-Binary.html#lines" target="_blank">http://haddocks.fpcomplete.com/fp/7.4.2/20130313-1/conduit/src/Data-Conduit-Binary.html#lines</a></div>




</div><div class="gmail_extra"><br><br><div class="gmail_quote"><div><div>On Mon, Apr 8, 2013 at 8:44 AM, Magicloud Magiclouds <span dir="ltr">&lt;<a href="mailto:magicloud.magiclouds@gmail.com" target="_blank">magicloud.magiclouds@gmail.com</a>&gt;</span> wrote:<br>




</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><div dir="ltr">Say I have code like below. If I comment the leftover in main, I got (Just &quot;L1\n&quot;, Just &quot;L2\n&quot;, Just &quot;L3\n&quot;, Just &quot;L4\n&quot;). But if I did not comment the leftover, then I got (Just &quot;L1\n&quot;, Just &quot;L1\n&quot;, Just &quot;&quot;, Just &quot;L2\n&quot;).<div>





Why is not it (Just &quot;L1\n&quot;, Just &quot;L1\n&quot;, Just &quot;L2\n&quot;, Just &quot;L3\n&quot;)?<br><div><br></div><div><div>takeLine :: (Monad m) =&gt; Consumer ByteString m (Maybe ByteString)</div><div>takeLine = do</div>





<div>&nbsp; mBS &lt;- await</div><div>&nbsp; case mBS of</div><div>&nbsp; &nbsp; Nothing -&gt; return Nothing</div><div>&nbsp; &nbsp; Just bs -&gt;</div><div>&nbsp; &nbsp; &nbsp; case DBS.elemIndex _lf bs of</div><div>&nbsp; &nbsp; &nbsp; &nbsp; Nothing -&gt; return $ Just bs</div><div>





&nbsp; &nbsp; &nbsp; &nbsp; Just i -&gt; do</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let (l, ls) = DBS.splitAt (i + 1) bs</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; leftover ls</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $ Just l</div><div><br></div><div><div>main = do</div><div>&nbsp; m &lt;- runResourceT $ sourceFile &quot;test.simple&quot; $$ (do</div>





<div>&nbsp; &nbsp; a &lt;- takeLine</div><div>&nbsp; &nbsp; leftover $ fromJust a</div><div>&nbsp; &nbsp; b &lt;- takeLine</div><div>&nbsp; &nbsp; c &lt;- takeLine</div><div>&nbsp; &nbsp; d &lt;- takeLine</div><div>&nbsp; &nbsp; return (a, b, c, d))</div><div>&nbsp; print m<span><font color="#888888"><br>




</font></span></div></div><span><font color="#888888">
<div><br></div>-- <br>竹密岂妨流水过<br>山高哪阻野云飞<br><br>And for G+, please use magiclouds#<a href="http://gmail.com" target="_blank">gmail.com</a>.
</font></span></div></div></div>
<br></div></div>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br>竹密岂妨流水过<br>山高哪阻野云飞<br><br>And for G+, please use magiclouds#<a href="http://gmail.com" target="_blank">gmail.com</a>.
</div>
</div></div></blockquote></div><br></div>