gpt4 book ai didi

haskell - react 香蕉 1.0 monadic API : How to define recursive behaviors now?

转载 作者:行者123 更新时间:2023-12-05 00:58:16 25 4
gpt4 key购买 nike

在 reactive-banana <1.0.0 中,这有效:

-- takes a start value, minimum and maximum value, flag whether counter is
-- cyclic, an increment and decrement event stream and returns a behavior
mkCounter :: (Enum a,Ord a) => a -> Maybe a -> Maybe a -> Bool
-> Event t b -> Event t c -> Behavior t a
mkCounter start minVal maxVal cyclic incE decE = counter
let incF curr | isNothing maxVal = succ
| Just maxv <- maxVal, curr<maxv = succ
| Just maxv <- maxVal, Just minv <- minVal, cyclic = const minv
| otherwise = id
decF curr | isNothing minVal = pred
| Just minv <- minVal, curr>minv = pred
| Just minv <- minVal, Just maxv <- maxVal, cyclic = const maxv
| otherwise = id
counter = accumB start $ ((incF <$> counter) <@ incE) `union`((decF <$> counter) <@ decE)

现在,counter 是根据自身定义的。但是在新的 monadic API 中,accumB 是一个 monadic 函数,这是我看不到如何继续的地方 - Moment 没有 MonadFix 实例,那么它现在是如何工作的?

由于显而易见的原因,这不起作用(“不在范围内:计数器”)

mkCounter :: (MonadMoment m,Enum a,Ord a) => a -> Maybe a -> Maybe a -> Bool
-> Event b -> Event c -> m (Behavior a)
mkCounter start minVal maxVal cyclic incE decE = do
-- .. same as above ..
counter <- accumB start $ unions [((incF <$> counter) <@ incE)
,((decF <$> counter) <@ decE)]
return counter

现在正确的做法是什么?提前致谢!

最佳答案

好吧,这是一个愚蠢的错误。我只需要添加 MonadFix m 作为约束,因为我没有直接使用 Moment Monad:

mkCounter :: (MonadFix m,MonadMoment m,Enum a,Ord a) => a -> Maybe a -> Maybe a -> Bool
-> Event b -> Event c -> m (Behavior a)

然后使用 mdo 就可以正常工作了。

关于haskell - react 香蕉 1.0 monadic API : How to define recursive behaviors now?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33001718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com