我有一个小的Haskell Pipe
打印出它运行了多少次:
counterPipe :: Pipe String String IO r
counterPipe = go 0
where
go n = do
await >>= yield
let n' = succ n
liftIO $ putStrLn $ "Chunk " ++ show n'
go n'
我希望能够在处理完最后一个块后打印出一条消息,并可能执行其他任务 . 我该怎么做呢?
1 回答
我能够通过将
counterPipe
的输入类型更改为Maybe String
并在上游管道完成后注入额外的Nothing
来实现此功能:驱动示例:
我认为这种模式可以抽象成类似的东西
所以你可以写
无需更改原始
counterPipe
定义;但我之前从未使用Pipes
(上面的解决方案只是通过查看类型和播放类型 - 多米诺骨牌来计算出来)所以我没有设法写whileJust
(签名可能过于通用,我无法想象出) .