我想实施某种类型的反应性香蕉事件抑制。它应该起作用,以使得如果从最后一次通过的事件到达的时间少于三秒,事件就不会通过。如果它没有通过,那么它将被存储并在最后一次触发事件的增量秒后触发。反应性香蕉节流事件
下面是一个程序,它实现了这个时间戳数字列表。将它翻译成反应香蕉是否可能?
另外,在反应式香蕉中,如何在某些其他事件进入后x秒钟内发射事件?
module Main where import Data.List -- 1 second throtling -- logic is to never output a value before 1 second has passed since last value was outputed. main :: IO() main = print $ test [ (0.0, 1.0), (1.1, 2.0), (1.5,3.0), (1.7,4.0), (2.2, 5.0) ] --should output [ (0.0, 1.0), (1.1, 2.0), (2.1,4.0), (3.1, 5.0) ] test :: [(Double,Double)] -> [(Double,Double)] test list = g v (concat xs) where (v, xs) = mapAccumL f (-50,Nothing) list g (t, Just x) ys = ys ++ [ (t+1,x) ] g _ ys = ys f (lasttime, Just holdvalue) (t,x) = if t > (lasttime+1) then if t > (lasttime + 2) then ((t, Nothing), [ (lasttime+1,holdvalue), (t,x)]) else ((lasttime+1, Just x) , [ (lasttime+1,holdvalue) ]) else ((lasttime, Just x), []) f (lasttime, Nothing) (t,x) = if t > (lasttime+1) then ((t,Nothing) , [ (t, x) ]) else ((lasttime, Just x), [])
如果您对IO不满意,您可以将计时器实现为侦听包含消息的事件(“start”,“stop”,“reset”)并返回另一个事件。一般来说,我建议将使用'reactimate'的函数放入'NetworkDescription' monad中,即'throttle :: ... - > NetworkDescription t(Event ta)',而不是'throttle :: .. - >(事件ta,NetworkDescription t())'。 –
好吧,这使得它确实更清洁,并且在语法方面与使用throttle :: Event t a - > Int - > Event t a(只是使用< - 而不是在do语句中放入)没有多大区别。我已用您的建议重新实施:https://gist.github.com/2905841。我觉得我对这个解决方案很满意。 –