Haskell 无模式匹配的管道选择
这与加布里埃尔对这个问题的反应密切相关。我已经编写了一个函数,它提供了类似于Haskell 无模式匹配的管道选择,haskell,haskell-pipes,Haskell,Haskell Pipes,这与加布里埃尔对这个问题的反应密切相关。我已经编写了一个函数,它提供了类似于箭头选项中的| |函数的功能,但用于代理(来自管道库)。它与模式匹配,并具有五个相互递归的函数。我想找到一个替代实现,它使用Pipes.Core中的函数来代替模式匹配 我首先在另一个答案中尝试了left的实现。看起来是这样的: newtype Edge m r a b = Edge { unEdge :: a -> Pipe a b m r } instance (Monad m) => ArrowChoi
箭头选项
中的| |
函数的功能,但用于代理
(来自管道
库)。它与模式匹配,并具有五个相互递归的函数。我想找到一个替代实现,它使用Pipes.Core
中的函数来代替模式匹配
我首先在另一个答案中尝试了left
的实现。看起来是这样的:
newtype Edge m r a b = Edge { unEdge :: a -> Pipe a b m r }
instance (Monad m) => ArrowChoice (Edge m r) where
left (Edge k) = Edge (bef >=> (up \>\ (k />/ dn)))
where
bef x = case x of
Left b -> return b
Right d -> do
_ <- respond (Right d)
x2 <- request ()
bef x2
up () = do
x <- request ()
bef x
dn c = respond (Left c)
看看如何在没有模式匹配的情况下实现这两种方法都会很有帮助。谢谢,如果我能澄清什么,请告诉我
proxyLeft :: Monad m
=> (a -> Pipes.Proxy y a y b m r)
-> Either a x
-> Pipes.Proxy y (Either a x) y (Either b x) m r
proxyLeft k = bef >=> (up \>\ (k />/ dn))
where
bef x = case x of
Left b -> return b
Right d -> do
a <- respond (Right d)
x2 <- request a
bef x2
up a = do
x <- request a
bef x
dn c = respond (Left c)
downstreamOr ::
Monad m
=> (b' -> Pipes.Proxy a' a b' b m r)
-> (c' -> Pipes.Proxy a' a c' b m r)
-> Either b' c'
-> Pipes.Proxy a' a (Either b' c') b m r
downstreamOr f g e = case e of
Left b' -> case f b' of
Pipes.Request a' k -> Pipes.Request a' (downstreamOrLeft k g)
Pipes.Respond b k -> Pipes.Respond b (downstreamOr k g)
Pipes.M m -> Pipes.M $ m >>= \p -> return $ downstreamLeftM p g
Pipes.Pure r -> Pipes.Pure r
Right c' -> case g c' of
Pipes.Request a' k -> Pipes.Request a' (downstreamOrRight f k)
Pipes.Respond b k -> Pipes.Respond b (downstreamOr f k)
Pipes.M m -> Pipes.M $ m >>= \p -> return $ downstreamRightM f p
Pipes.Pure r -> Pipes.Pure r
downstreamOrLeft ::
Monad m
=> (a -> Pipes.Proxy a' a b' b m r)
-> (c' -> Pipes.Proxy a' a c' b m r)
-> a
-> Pipes.Proxy a' a (Either b' c') b m r
downstreamOrLeft f g a = case f a of
Pipes.Request a' k -> Pipes.Request a' (downstreamOrLeft k g)
Pipes.Respond b k -> Pipes.Respond b (downstreamOr k g)
Pipes.M m -> Pipes.M $ m >>= \p -> return $ downstreamLeftM p g
Pipes.Pure r -> Pipes.Pure r
downstreamOrRight ::
Monad m
=> (b' -> Pipes.Proxy a' a b' b m r)
-> (a -> Pipes.Proxy a' a c' b m r)
-> a
-> Pipes.Proxy a' a (Either b' c') b m r
downstreamOrRight f g a = case g a of
Pipes.Request a' k -> Pipes.Request a' (downstreamOrRight f k)
Pipes.Respond b k -> Pipes.Respond b (downstreamOr f k)
Pipes.M m -> Pipes.M $ m >>= \p -> return $ downstreamRightM f p
Pipes.Pure r -> Pipes.Pure r
downstreamLeftM ::
Monad m
=> Pipes.Proxy a' a b' b m r
-> (c' -> Pipes.Proxy a' a c' b m r)
-> Pipes.Proxy a' a (Either b' c') b m r
downstreamLeftM p g = case p of
Pipes.Pure r -> Pipes.Pure r
Pipes.Request a' k -> Pipes.Request a' (downstreamOrLeft k g)
Pipes.Respond b k -> Pipes.Respond b (downstreamOr k g)
Pipes.M m -> Pipes.M $ m >>= \p -> return $ downstreamLeftM p g
downstreamRightM ::
Monad m
=> (b' -> Pipes.Proxy a' a b' b m r)
-> Pipes.Proxy a' a c' b m r
-> Pipes.Proxy a' a (Either b' c') b m r
downstreamRightM f p = case p of
Pipes.Pure r -> Pipes.Pure r
Pipes.Respond a' k -> Pipes.Respond a' (downstreamOr f k)
Pipes.Request a' k -> Pipes.Request a' (downstreamOrRight f k)
Pipes.M m -> Pipes.M $ m >>= \p -> return $ downstreamRightM f p