Is there a network equivalent of a stepper?

Reactive-banana has a function with a name stepper(type MonadMoment m => a -> Event a -> m (Behavior a)) that converts the event to behavior, where the behavior value is the value of the last event or the initial value if the event has not yet occurred.

In Conal Elliotโ€™s conversation, whose name I donโ€™t remember, he presents this as one of the fundamental operations on events and behavior. However, I can not find a similar function on the network. With my limited understanding of netwire, I expect it to be of type:

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

  • Does netwire have an equivalent function?
  • If not, is there a reason this is not possible?
  • Is anything like this possible, i.e. ways to transform events into behavior?
+4
source share
2 answers

The function I was looking for is called in . holdControl.Wire.Interval

This does not require an initial value, since the wire is blocked until the first event is received. If necessary, it can be implemented as follows.

stepper init = hold <|> pure init
+2
source

I can only guess why netwire does not provide this. Everything in Control.Wire.Eventstores results in Event, retaining knowledge of when they occur.

You can exit Eventusing one of the switching methods Control.Wire.Switch. You are 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)

The above code is pureused both a -> Wire s e m () ato create trivial wires.

0
source

Source: https://habr.com/ru/post/1658365/


All Articles