Skip to content

Instantly share code, notes, and snippets.

@mightybyte
Created May 21, 2017 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mightybyte/d3afb1806992c73ee4f1eb491898e2da to your computer and use it in GitHub Desktop.
Save mightybyte/d3afb1806992c73ee4f1eb491898e2da to your computer and use it in GitHub Desktop.
{-# LANGUAGE DeriveFunctor #-}
module Form where
import Control.Lens
import Data.Default
import Data.Functor.Compose
import Data.Monoid
import Reflex
data FormInput t a = FormInput
{ _formInput_initialValue :: Maybe a
, _formInput_setValue :: Event t (Maybe a)
}
instance Reflex t => Functor (FormInput t) where
fmap f (FormInput iv sv) = FormInput (f <$> iv) (fmap f <$> sv)
data FormOutput t a = FormOutput
{ _formOutput_value :: Dynamic t (Maybe a)
, _formOutput_change :: Event t (Maybe a)
}
instance Reflex t => Default (FormOutput t a) where
def = FormOutput (constDyn Nothing) never
instance Reflex t => Functor (FormOutput t) where
fmap f (FormOutput v c) = FormOutput (fmap f <$> v) (fmap f <$> c)
instance Reflex t => Applicative (FormOutput t) where
pure a = FormOutput (constDyn $ Just a) never
(FormOutput vf cf) <*> (FormOutput va ca) = FormOutput v
(tagPromptlyDyn v $ leftmost [() <$ cf, () <$ ca])
where
v = getCompose $ Compose vf <*> Compose va
newtype FormT i o t m a = FormT { runFormT :: FormInput t i -> m (FormOutput t o, a) }
zoomForm :: Reflex t => (b -> v) -> FormT v o t m a -> FormT b o t m a
zoomForm l f = FormT $ \fi -> runFormT f (l <$> fi)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment