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 sem (Event a) a
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
I can only guess why netwire doesn't provide that. Everything in Control.Wire.Event
keeps the results in Event
s preserving the knowledge of when they are happening.
You can get out of Event
s 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 sem () a
to create trivial wires.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.