Skip to content
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

Include a helper function to move a MonadHold to the outside of a dynamic. #123

Open
michaelmesser opened this issue Jul 24, 2017 · 4 comments

Comments

@michaelmesser
Copy link

The weird type signature is required because m1 must be PushM and m.

f :: forall t m a. (Reflex t, MonadHold t m) => (forall m1. (MonadHold t m1) => Dynamic t (m1 a)) -> m (Dynamic t a)
f d = do
    let events = updated d & pushAlways id
    _initial <- sample $ current d
    initial <- _initial
    holdDyn initial events
@cgibbard
Copy link
Collaborator

How is this being used? Do you find you need it very often?

The requirement of sampling the Dynamic immediately in order to execute the action initially is something that we usually try to avoid in widget code because it's stricter than most other operations, and can result in loops that are trickier than usual to decipher.

@michaelmesser
Copy link
Author

@cgibbard I found a use for it when I had Dynamic t (Incremental t p) and I needed to transform the incremental in a way that required monad hold.

@ryantrinkle
Copy link
Member

@2426021684 This is very similar to runWithReplace. Would that be helpful here?

One of the rules we try to abide by in all our widget code is not to be strict in any Behaviors (including the current of a Dynamic). It can lead to awkward code sometimes, but it also seems to make people far less likely to write code that loops, so we've considered it an acceptable tradeoff. On the other hand, there may be some other approach we could use to avoid loops. If you have any ideas, let me know!

@michaelmesser
Copy link
Author

@ryantrinkle
This function appears to do the same thing as f

f2 :: (Adjustable t m, MonadHold t m) => Dynamic t (m a) -> m (Dynamic t a)
f2 d = uncurry holdDyn =<< runWithReplace (join (sample (current d))) (updated d)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants