How to share state between pipes consumers without using ioref?

53 views
Skip to first unread message

Ray Qiu

unread,
Apr 29, 2015, 12:36:32 AM4/29/15
to haskel...@googlegroups.com
How to share state between pipes Consumers/Pipes without using ioref?  Thanks!

Gabriel Gonzalez

unread,
Apr 29, 2015, 12:45:07 AM4/29/15
to haskel...@googlegroups.com, ray...@gmail.com
Add a `StateT` layer to each component's base monad.  They will then share that state.  Here is an example:

    import Control.Monad (forever)
    import Control.Monad.Trans.State
    import Pipes
   
    producer :: Producer Int (StateT Int IO) r
    producer = forever (do
        n <- lift get
        lift (put $! n + 1)
        yield n )
   
    consumer :: Consumer Int (StateT Int IO) r
    consumer = forever (do
        n <- await
        lift (lift (print n))
        lift (modify (+ 1)) )
   
    main :: IO ()
    main = evalStateT (runEffect (producer >-> consumer)) 0

That will print:

    0
    2
    4
    6
    ...




On 04/28/2015 09:36 PM, Ray Qiu wrote:
How to share state between pipes Consumers/Pipes without using ioref?  Thanks!
--
You received this message because you are subscribed to the Google Groups "Haskell Pipes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to haskell-pipe...@googlegroups.com.
To post to this group, send email to haskel...@googlegroups.com.

Ray Qiu

unread,
Apr 29, 2015, 12:54:06 AM4/29/15
to haskel...@googlegroups.com, ray...@gmail.com
Nice!  Thanks!

Gabriel Gonzalez

unread,
Apr 29, 2015, 12:54:28 AM4/29/15
to haskel...@googlegroups.com, ray...@gmail.com
You're welcome!
Reply all
Reply to author
Forward
0 new messages