[Haskell-beginners] First steps with Streaming library

aquagnu aquagnu at gmail.com
Sat May 27 15:30:28 UTC 2017


I'm trying to start working with Streaming package. First attempt
is to simulate source of stream items (I call the function "gen")
and some processor ("proc"). Both should be stateful: to be able
to sabe some info about processing steps, etc.

  ...
  import Streaming
  import qualified Streaming.Prelude as S
  ...

  gen :: S.Stream (S.Of Int) IO [String]
  gen = do
    S.yield 1000
    S.yield 2000
    x <- lift getLine
    return ["a", "b", "c", x] -- results

  proc :: S.Stream (S.Of Int) IO [String] -> S.Stream (S.Of Int) IO [String]
  proc str = do
    e <- str
    lift $ print "Enter x:"
    x <- lift getLine
    return $ e ++ [" -- " ++ x] -- put stream items in result
  
  main :: IO
  main = do
    s <- S.mapM_ print $ S.map show gen
    p <- S.mapM_ print $ proc gen
    putStr "s: " >> print s
    putStr "p: " >> print p

And I try to simulate "piping" between "gen" and "proc", seems that
function application is enought to compose producers and consumers. But
this snippet is not correct: "e <- str" extracts element not from
stream, but from results (of "gen"). Even more, I don't know how to
"yield" new items, based on stream items. Something like "await" of
Conduit, or like in Python "for e in str: ... yield modified(e)...". Is
it possible to do it with "do" notation?

---
Best,
  Paul


More information about the Beginners mailing list