<div dir="ltr">Yes, that'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"><<a href="mailto:magicloud.magiclouds@gmail.com" target="_blank">magicloud.magiclouds@gmail.com</a>></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've learnt the code of "lines". 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"><<a href="mailto:michael@snoyman.com" target="_blank">michael@snoyman.com</a>></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's a bug in your implementation of takeLine I believe. It doesn't take into account that lines can span multiple chunks. When you call takeLine the first time, you get "L1\n". leftover puts a chunk with exactly those contents back. When you call takeLine the second time, it gets the chunk "L1\n", and your splitAt gives you back "L1\n" and "". The "" is then leftover, and the next call to takeLine gets it.<div>
<br></div><div>Your takeLine needs to include logic saying "there's no newline in this chunk at all, let's get the next chunk and try that." 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] <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"><<a href="mailto:magicloud.magiclouds@gmail.com" target="_blank">magicloud.magiclouds@gmail.com</a>></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 "L1\n", Just "L2\n", Just "L3\n", Just "L4\n"). But if I did not comment the leftover, then I got (Just "L1\n", Just "L1\n", Just "", Just "L2\n").<div>
Why is not it (Just "L1\n", Just "L1\n", Just "L2\n", Just "L3\n")?<br><div><br></div><div><div>takeLine :: (Monad m) => Consumer ByteString m (Maybe ByteString)</div><div>takeLine = do</div>
<div> mBS <- await</div><div> case mBS of</div><div> Nothing -> return Nothing</div><div> Just bs -></div><div> case DBS.elemIndex _lf bs of</div><div> Nothing -> return $ Just bs</div><div>
Just i -> do</div><div> let (l, ls) = DBS.splitAt (i + 1) bs</div><div> leftover ls</div><div> return $ Just l</div><div><br></div><div><div>main = do</div><div> m <- runResourceT $ sourceFile "test.simple" $$ (do</div>
<div> a <- takeLine</div><div> leftover $ fromJust a</div><div> b <- takeLine</div><div> c <- takeLine</div><div> d <- takeLine</div><div> return (a, b, c, d))</div><div> 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>