[Haskell-beginners] reactiva-banana : how to implement a certain type of throttling

Miguel Negrao miguel.negrao-lists at friendlyvirus.org
Sat Jun 2 13:38:15 CEST 2012


Hi list,

I would like to implement a certain type of throttling of events in reactive-banana. It should work such that an event is not let through if arrives at less then delta seconds from the last event that passed through. If it is not let through then it is stored and is fired after delta seconds from the last fired event. 

Below is a program that implements this for lists of time stamped numbers. Would it be possible to translate this to reactive-banana ?

Also, in reactive-banana how do I fire an event x seconds after some other event comes in. The way I would do it in scala’s reactive-web is to start a timer that fires x seconds after it is started when an event comes in using dynamic event switching . It seems to me this would not be possible in reactive-banana because starting a timer is an IO operation, so I assume it can’t be done inside the event/behavior logic, right ?

best,
Miguel Negrão

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), [] )




More information about the Beginners mailing list