[Haskell-cafe] [Conduit] weird action of leftover.

Magicloud Magiclouds magicloud.magiclouds at gmail.com
Mon Apr 8 07:44:16 CEST 2013


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").
Why is not it (Just "L1\n", Just "L1\n", Just "L2\n", Just "L3\n")?

takeLine :: (Monad m) => Consumer ByteString m (Maybe ByteString)
takeLine = do
  mBS <- await
  case mBS of
    Nothing -> return Nothing
    Just bs ->
      case DBS.elemIndex _lf bs of
        Nothing -> return $ Just bs
        Just i -> do
          let (l, ls) = DBS.splitAt (i + 1) bs
          leftover ls
          return $ Just l

main = do
  m <- runResourceT $ sourceFile "test.simple" $$ (do
    a <- takeLine
    leftover $ fromJust a
    b <- takeLine
    c <- takeLine
    d <- takeLine
    return (a, b, c, d))
  print m

-- 
竹密岂妨流水过
山高哪阻野云飞

And for G+, please use magiclouds#gmail.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20130408/eac15c1e/attachment.htm>


More information about the Haskell-Cafe mailing list