<div dir="rtl"><div dir="ltr">Hi,</div><div dir="ltr">Thanks, I'm feeling kinda dumb because I forgot this is applicative, but yet feeling amazed on how great Haskell is to let you do stuff like that.</div><div dir="ltr">Your nose may not have betrayed you, I'm using these forms to show rows from a database with added buttons to manage it from the web. On the other hand, I might be really bad at explaining my questions </div><div dir="ltr">:)</div></div><br><div class="gmail_quote"><div dir="rtl">בתאריך יום ב׳, 24 ביולי 2017 ב-15:54 מאת MarLinn <<a href="mailto:monkleyon@gmail.com">monkleyon@gmail.com</a>>:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 2017-07-24 12:54, Yotam Ohad wrote:<br>
><br>
> […]<br>
><br>
> Is there a better way to do it?<br>
<br>
Yes. AForm is Applicative. So you should just be able to use other<br>
applicative combinators.<br>
<br>
For example (untested):<br>
<br>
fooAForm :: Int -> AForm Handler [Foo]<br>
fooAForm count = traverse makeFooField [1..count]<br>
where<br>
<br>
makeFooField :: Int -> AForm Handler Foo<br>
makeFooField n = areq (selectFieldList foos) (makeFooName n) Nothing<br>
<br>
<br>
foos :: [(Text, Role)]<br>
foos = first tshow . join (,) <$> [minBound..]<br>
<br>
Note that traverse works on any Traversable (Duh.), and the usual maps<br>
are all Traversable, so you can easily adapt this to return, say, a<br>
(Map String Foo) or a (Map FooName Foo). And similar methods work for<br>
monadic forms.<br>
<br>
But: The way you asked the question smells like there might be something<br>
bad in either the user interface or the structural design. Maybe you<br>
want just one form with six fields plus CSS to conditionally hide some<br>
of them? Maybe you want one form with n fields plus an additional field<br>
to select the n? (Monadic fields should be able to do that.) Maybe<br>
there's no real maximum limit so you need just one field generator but a<br>
more complex front-end? I'm not saying you're doing it wrong, it's just<br>
that my nose is itching.<br>
<br>
Cheers.<br>
<br>
<br>
> This is how I defined the forms:<br>
> foo2AForm :: AForm Handler Foo2<br>
><br>
> foo2AForm = Foo2<br>
> <$> areq (selectFieldList foos) "foo1" Nothing<br>
> <*> areq (selectFieldList foos) "foo2" Nothing<br>
> where<br>
> foos:: [(Text, Role)]<br>
> foos= map (pack . show &&& id) [minBound..]<br>
><br>
> […]<br>
><br>
</blockquote></div>