繁体   English   中英

Pipes (Haskell lib) - 具有不同 state monad 的管道

[英]Pipes (Haskell lib) - piping pipes with different state monad

我的目标是使最后产生的值等于 80 (40 + 40)(参见下面的代码)...

import Pipes
import Pipes.Prelude
import Pipes.Lift
import Control.Monad.State.Strict

data Input = A Integer | B Integer | C Integer

main :: IO ()
main = runEffect $ each [A 10,B 2,C 3,A 40,A 40] >-> pipeline >-> print


pipeline :: Pipe Input Integer IO ()
pipeline = for cat $ \case
  A x -> yield x >-> accumulate
  B x -> yield x
  C x -> yield x

accumulate :: Pipe Integer Integer IO ()
accumulate = evalStateP 0 accumulate'


accumulate' :: Pipe Integer Integer (StateT Integer IO) ()
accumulate' = go
  where
    go = do
        x <- await
        lift $ modify (+x)
        r <- lift get
        yield r
        go

在此示例中,输入A没有累积... yield x >-> accumulate确实符合我的预期,stream 每次都是新的...

具有不同 state monad 的管道管道顺序运行良好,但在这里我想以某种方式将它们嵌套在案例模式中(就像子流一样)......

问题是您过早地调用evalStateP ,丢弃了 state 您想要在调用中保留的accumulate 尝试这样的事情:

pipeline :: Pipe Input Integer IO ()
pipeline = evalStateP 0 $ for cat $ \case
  A x -> yield x >-> accumulate
  B x -> yield x
  C x -> yield x

accumulate :: Pipe Integer Integer (StateT Integer IO) ()
accumulate = for cat $ \x -> do
        modify (+x)
        r <- get
        yield r

请注意, Proxy有一个MonadState实例,因此如果您使用mtl ,则无需手动解除 state 操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM