-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pure function for Behavior updates #151
Comments
Sorry for the late reply. Semantically, Of course, one can debate whether these semantics are a good idea. I do, however, think that they are: They eliminate a source of bugs. A Behavior can have a "change" but still have the same value before and after. The current semantics make it impossible for pure code to distinguish between the two, and I think that's a good thing. This is also explained here (point 1). |
@HeinrichApfelmus Maybe one really don't need such a function.. Let me describe my problem from a scratch, as it is related to your article. So we have only inputs from window (e.g. resolution and mouse clicks) as reactive-banana AddHandlers and reactimate (draw <$> ... <@ eRedraw) with draw :: ... -> IO () for output. All logic for the widget should be pure and live in its encapsulated events and behaviors.
Here we can't pass bWidgetVisible to function, because we need to know eHide moment to turn off bWidgetDragged. Based on your experience, can you suggest the right approach for such pure-FRP-widget constructor function(s)? P.S. I also tried to do fully dynamic widget (oppositely to having visibility), but switchE startled me during my first attempt: #152. |
I think something like Dynamic is useful here, which is just a -- constructor kept abstract
data Dynamic a
= Dynamic (Behavior a) (Event a)
deriving Functor
current :: Dynamic a -> Behavior a
current (Dynamic x _) = x
updates :: Dynamic a -> Event a
updates (Dynamic _ x) = x
accumD :: MonadMoment m => a -> Event (a -> a) -> m (Dynamic a)
accumD x fs = do
(e, b) <- mapAccum x ((\f y -> dup (f y)) <$> fs)
pure (Dynamic b e)
where
dup z = (z, z) |
Relatedly, I have a need of a function Currently the user has to find the plainChanges example in Frameworks and understand it well enough (or not) to use it safely, in order to make something like that. It seems that an accumulator, that only runs the function when the Behavior changed to a different value, would be safe to include. |
What would this function do? Presumably it's not |
I think that a function detectEdges :: Behavior (Maybe a) → Event (Future a) is possible within the semantics of continuous time — we can't tell when a Behavior "changes" in general, but we can detect when a |
Can we have pure function, similar to changes with type like Behavior a -> Moment (Event (Future a)) (or Behavior a -> Moment (Event a), or even Behavior a -> Event a)?
E.g. Sodium library has such a function: updates.
There were some related questions on stackoverflow: 1, 2, 3.
At now proposed solutions to these kind of problems are new types:
But these types seem to be redundant and not as modular as simple Behavior type.
The text was updated successfully, but these errors were encountered: