Search code examples
haskellfrpreactive-banana

Filter duplicate events in reactive-banana


Let's say I have

x :: Event t (A,B)

I can get the first component of it:

fst <$> x :: Event t A

However, this event will fire even when the first component doesn't change. I want to avoid that, because it would trigger an expensive recomputation.

A is an instance of Eq, so I want to be able to remove the successive events where the first component is not changed compared to its last value.

Ideally, I'd like a function

filterDups :: Eq a => Event t a -> Event t a

which would do that without resorting to the Moment monad. Is it possible? Or what's the best way to do this?


Solution

  • You have to remember information about the history of the event to do what you want. As other answers have already mentioned, you can use accumE for that purpose. Here a succinct definition:

    unique :: Eq a => Event t a -> Event t a
    unique = filterJust . accumE Nothing
           . fmap (\a acc -> if Just a == acc then Nothing else Just a)