注意
mapAccum :: acc -> Event t (acc -> (x, acc)) -> (Event t x, Behavior t acc)
所以它需要一个初始值:: acc 来累积,以及一个产生一个函数的事件
更新累积值,同时产生输出值::x。 (通常,您会通过<$> 部分应用某些功能来制作这样的事件。)
结果,您会得到一个新事件,该事件会在您的 x 值出现时触发它们,并且包含
您当前的累积值。
如果您有一个事件并且想要做出相关的行为和事件,请使用mapAccum。
例如,在来自your other question 的问题域中,假设您有一个事件eTime :: Event t Int 异常触发,并且您想计算eDeltaTime :: Event t Int 的差异和bTimeAgain :: Behaviour t Int
当前使用的时间:
type Time = Int
type DeltaTime = Time
getDelta :: Time -> Time -> (DeltaTime,Time)
getDelta new old = (new-old,new)
我本可以写 getDelta new = \old -> (new-old,new) 以使下一步更清晰:
deltaMaker :: Event t (Time -> (DeltaTime,Time))
deltaMaker = getDelta <$> eTime
(eDeltaT,bTimeAgain) = mapAccum 0 $ deltaMaker
在这种情况下,bTimeAgain 将是与eTime 中的事件具有相同值的行为。有时候是这样的
因为我的getDelta 函数将new 直接从eTime 不变地传递到acc 值。
(如果我想单独使用bTimeAgain,我会使用stepper :: a -> Event t a -> Behaviour t a。)
如果我不需要bTimeAgain,我可以写(eDeltaT,_) = mapAccum 0 $ deltaMaker。