[Haskell-cafe] How to compose operations in streaming

☂Josh Chia (謝任中) joshchia at gmail.com
Wed Jun 3 07:04:59 UTC 2020


I have a question about the streaming package.

Let's say I have str :: Stream (Of String) IO ()
I want to putStrLn each element and then print the length of str, so I
could do this:

import Streaming
import qualified Streaming.Prelude as SP

doit :: Stream (Of String) IO () -> IO ()
doit str =
  do
    SP.mapM_ putStrLn str
    SP.length_ str >>= print

However, will this result in doing 2 passes over str so that the memory
requirement is not constant? If so, is there a simple way to combine the
two actions so that only pass is needed? I could probably use a counter in
a StateT but that's not really simple. More generally, are there techniques
for combining folds like how one would with the foldl package?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20200603/37e951e9/attachment.html>


More information about the Haskell-Cafe mailing list