jaspervdj / digestive-functors

A general way to consume input using applicative functors
149 stars 71 forks source link

Provide an "optional" combinator #33

Closed mightybyte closed 12 years ago

mightybyte commented 12 years ago
optional :: Form v m a -> Form v m (Maybe a)

The way I envision this working is if there is no input, then the form succeeds with a value of Nothing. If there is input, then the validator runs. In the ultimate result, you'll have the fairly ugly Maybe (Maybe a) where the outer Maybe represents whether there were errors and the inner Maybe represents whether there was data. It's pretty easy to write this combinator for primitive fields, but it's more difficult for compound forms. I imagine the semantics would be that the final result would be Nothing only if all of the subforms were completely empty. Otherwise it would go to the validator.

I'm not sure whether could implement this by recursively changing all subforms to return Maybe values or if it would need an augmented Result type such as:

data Result v a =
  Success a |
  Error v |
  NoData
jaspervdj commented 12 years ago

Semantics: what would:

optional (pure "Hello")

give?

mightybyte commented 12 years ago

Is there a reason it wouldn't be

pure (Just "Hello")

?