4

Reactive-banana has a function named stepper (of type MonadMoment m => a -> Event a -> m (Behavior a)), which converts an event into a behaviour where the behaviour's value is the value of the last event, or the initial value if no event has occurred yet.

In a talk by Conal Elliott, the name of which I cannot remember, he presents this as being one of the fundamental operations on events and behaviours. However, I cannot find a similar function in netwire. With my limited understanding of netwire, I would expect it to have type:

a -> Wire s e m (Event a) a

  • Does netwire have an equivalent function?
  • If not, is there any reason why this is not possible?
  • Is anything similar possible, i.e. ways of converting events into behaviours?
Cameron Martin
  • 5,764
  • 1
  • 35
  • 52

2 Answers2

2

The function I was looking for is called hold, in Control.Wire.Interval.

This does not need an initial value, because the wire inhibits until the first event is received. If this behaviour is needed, it can be implemented like so

stepper init = hold <|> pure init
Cameron Martin
  • 5,764
  • 1
  • 35
  • 52
0

I can only guess why netwire doesn't provide that. Everything in Control.Wire.Event keeps the results in Events preserving the knowledge of when they are happening.

You can get out of Events by using one of the methods of switching in Control.Wire.Switch. You're looking for rSwitch.

-- Beware: untested, untype-checked code
stepper :: (Monad m) => a -> Wire s e m (Event a) a
stepper init = switcher . source
  where
     -- source :: Wire s e m (Event a) ((), Event (Wire s e m () a))
     source = arr (\e -> ((), pure <$> e))
     -- switcher :: Wire s e m ((), Event (Wire s e m () a)) a
     switcher = rSwitch (pure init)

In the code above pure is used as a -> Wire s e m () a to create trivial wires.

Cirdec
  • 23,492
  • 2
  • 45
  • 94