You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This isn't really an issue, I just wanted to share something I thought was neat.
I wanted to be able to build a widget that constructs a bigger structure out of widgets that construct it's parts. So basically a form builder. After a few attempts and remembering a trick with existential states that the foldl library uses, I came up with this:
dataFormawhereForm::state-> (state-> [WidgetHTML (Maybeevent)]) -> (state->event->state) -> (state->a) ->FormainstanceFunctorFormwherefmap f (Form s w u p) =Form s w u (f . p)
instanceApplicativeFormwherepure a =Form() (\()->[]) (\()()->()) (\()-> a)
Form s1 w1 u1 p1 <*>Form s2 w2 u2 p2 =Form (s1, s2) w u p
where
w (s1', s2') = (fmap (fmapLeft) <$> w1 s1') <> (fmap (fmapRight) <$> w2 s2')
u (s1', s2') (Left e1) = (u1 s1' e1, s2')
u (s1', s2') (Right e2) = (s1', u2 s2' e2)
p (s1', s2') = p1 s1' (p2 s2')
formToWidget::Forma->WidgetHTMLa
formToWidget (Form s w u p) = go s
where
go s' =do
e <-div[] (w s')
case e ofNothing->return (p s')
Just e -> go (u s' e)
So basically, a Form is a widget with an internal state that it knows how to update. When you applicatively combine two forms, you get a bigger form that runs the widgets in parallel until one of them signals that it's done (by returning Nothing). It then projects out it's internal state into some structure.
This isn't really an issue, I just wanted to share something I thought was neat.
I wanted to be able to build a widget that constructs a bigger structure out of widgets that construct it's parts. So basically a form builder. After a few attempts and remembering a trick with existential states that the
foldl
library uses, I came up with this:So basically, a
Form
is a widget with an internal state that it knows how to update. When you applicatively combine two forms, you get a bigger form that runs the widgets in parallel until one of them signals that it's done (by returningNothing
). It then projects out it's internal state into some structure.Here's how it can be used:
In a sense, a
Form
is a widget with parallel composition instead of sequential.The text was updated successfully, but these errors were encountered: