在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中,accumB是一个一元函数,这就是我不知道如何继续的地方-- 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现在做这件事的正确方法是什么?提前感谢!
发布于 2015-10-08 05:10:01
好吧,这是个愚蠢的错误。我只需要添加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一起工作。
https://stackoverflow.com/questions/33001718
复制相似问题