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

suggested helper function: maybeWidget #260

Open
rprije opened this issue Dec 4, 2018 · 2 comments
Open

suggested helper function: maybeWidget #260

rprije opened this issue Dec 4, 2018 · 2 comments

Comments

@rprije
Copy link

rprije commented Dec 4, 2018

I find this helper function useful:

maybeWidget :: (DomBuilder t m, MonadFix m, MonadHold t m)
=> m b
-> (Dynamic t a -> m b)
-> Dynamic t (Maybe a)
-> m (Dynamic t (m b))
maybeWidget handleNothing handleJust dynMaybe =
(fmap . fmap) (maybe handleNothing handleJust) $ maybeDyn dynMaybe

It's analogous to the maybe function in that it allows a Dynamic t (Maybe a) to dynamically switch between widgets depending on whether that maybe is a Nothing or a Just. I typically use it with dyn and switchEvent from reflex-frp/reflex#49

@dfordivam
Copy link
Member

This can also be in reflex along with Reflex.Collection APIs..

@alexfmpe
Copy link
Member

In addition to what @dfordivam said, it might be worth adding similar helpers, like

eitherWidget :: (DomBuilder t m, MonadFix m, MonadHold t m)
            => (Dynamic t a -> m c)
            -> (Dynamic t b -> m c)
            -> Dynamic t (Either a b)
            -> m (Dynamic t (m c))
eitherWidget handleLeft handleRight dynEither =
  (fmap . fmap) (either handleLeft handleRight) $ eitherDyn dynEither

or theseWidget (if theseDyn was a thing).

Maybe it can even be derived generically (https://github.com/reflex-frp/reflex/pull/106/files/173c1bf983b8a61b17c932230ee3757c0e154add#diff-2536923df364966db0a4fb4bc1d9eb4aR44)

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

4 participants