type の Conduit と typeConduit a m aの function があり(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 を呼び出しません。