Search code examples
haskellfrpelm

How do I merge signals in Helm?


I am working on a project using Helm, which is based on the Elm language.

I need to trigger an event based on which signal out of a pair of signals arrives first. In Elm, I would use the merge function, but I cannot find an equivalent in Helm. The closest I see is combine (in the signal library), which doesn't appear to do what I want. It seems that combine just takes a list of signals and makes them directly into a signal of lists, which is not quite what I'm looking for.

EDIT: specifically I am looking for a function with signature Signal a -> Signal a -> Signal a that takes the first signal to fire and discards the second.

What's the best way to accomplish this in Helm?


Solution

  • I think the best way would be finding out how Helm's Signals are implemented in terms of Elerea's stuff, and implementing merge in those more primitive terms. I'm afraid I can't help you there, because I don't know Helm well and I don't know Elerea at all.

    I can help you with a hacky solution based solely on Helm. You can use timestamp to distinguish different events from zipped signals:

    merge sigL sigR =
      let
        tsMerge (t1,v1) (t2,v2) =
            if t1 >= t2
              then v1
              else v2
      in
        tsMerge <~ timestamp sigL ~~ timestamp sigR