【发布时间】:2021-02-14 01:16:20
【问题描述】:
我有一个生产者p,类型为Producer Message IO (Producer SB.ByteString IO ())。
现在我需要选择性地跳过一些消息并选择性地处理一定数量的消息:
let p =
(processMBFile f >->
P.drop (optSkip opts)
>->
(case optLimit opts of
Nothing -> cat
Just n -> ptake n)
)
in
do
restp <- runEffect $ for p processMessage
我无法使用 Pipes.Prelude 中的 take,因为它返回 (),而我需要返回一个空的生产者。我快速解决它的方法是将take替换为我自己的实现ptake:
emptyP :: Producer SB.ByteString IO ()
emptyP = return ()
ptake :: Monad m => Int -> Pipe a a m (Producer SB.ByteString IO ())
ptake = go
where
go 0 = return emptyP
go n = do
a <- await
yield a
go (n-1)
我的问题是:是否有更优雅的方式来做到这一点?
【问题讨论】:
-
如果我没有忽略任何事情,因为你总是返回
emptyP,你的ptake可以写成ptake n = emptyP <$ take n -
我也很惊讶库中没有
emptyP的通用版本。
标签: haskell haskell-pipes