我有一个类型的 ConduitConduit a m a
和一个类型的函数(a -> Maybe a)
。我想运行该函数,然后如果它返回 Nothing,请使用 Conduit。也就是说,我想要一个类型的函数
maybePipe :: Conduit a m b -> (a -> Maybe b) -> Conduit a m b
或者,更受限制的类型
maybePipe :: Conduit a m a -> (a -> Maybe a) -> Conduit a m a
如果有帮助,我的具体情况如下:
我正在编写处理 IRC 消息的代码,并且我有一个函数:
runClient :: Conduit IRC.Message IO IRC.Message -> ClientSettings -> IO ()
runClient pipe address = runTCPClient' pipe' address where
pipe' = mapC IRC.decode $= concatMapC id $= pipe $= mapC IRC.encode $= mapC (++ "\r\n")
handlePings (IRC.Message (Just (IRC.Server serverName)) "PING" []) = Just $ IRC.pong serverName
handlePings (IRC.Message Nothing "PING" [server]) = Just $ IRC.pong server
handlePings (IRC.Message Nothing "PING" []) = Just $ IRC.pong (getHost address)
handlePings _ = Nothing
runTCPClient' :: Conduit ByteString IO ByteString -> ClientSettings -> IO ()
runTCPClient' pipe address = runTCPClient address runClient where
runClient appdata = appSource appdata $= linesUnboundedAsciiC $= pipe $$ appSink appdata
我希望能够maybePipe handlePings pipe
在该函数中执行(或等效)操作,因此当 IRC 消息是 ping 时,我们以 pong 响应并且不调用用户指定的 Conduit。