Вопрос

I have been working my way through this tutorial:

http://pbrisbin.com/posts/developing_web_applications_with_yesod/

 lemonadeForm :: Form Lemonade
 lemonadeForm = renderTable $ Lemonade
     <$> pure Nothing
     <*> areq (selectField optionsEnum) "Size" Nothing
     <*> pure 0.0
     <*> areq intField "Quantity" Nothing

I have several questions about this syntax.

1) It says a few lines later in the tutorial that "The lines that use pure provide values when processed, but don't actually show any fields." Does this mean that otherwise the form would not know the meaning of Nothing and 0.0?

2) What does the syntax <$> and <*> mean respectively?

Это было полезно?

Решение

What does the syntax <$> and <*> mean respectively?

pure and <*> are two class methods of type class Applicative, <$> is an infix synonym for fmap, which is one of the class methods of Functor.

Chatper 11 Functors, Applicative Functors and Monoids of Learn You a Haskell has a quite good introduction to Functor and Applicative, you may want to take a look.

Другие советы

The datatype of Lemonade is as follows:

data Lemonade = Lemonade
    { lemonadeOrder :: Maybe OrderId
    , lemonadeSize  :: Size
    , lemonadePrice :: Price
    , lemonadeQty   :: Qty
    }

The lines that use pure provide values when processed, but don't actually show any fields.

What this means is that when the form is displayed as HTML in the browser, the fields for which pure has been used will not be shown. Although the value will be used internally, it won't be shown to the user. In your example, the fields for lemonadeOrder and lemonadePrice will not be shown up in the form.

What does the syntax <$> and <*> mean respectively?

<$> is an alias for fmap. <*> is used for Applicative style programming. You can get to know more about them by inspecting it's type and by reading Typeclassopedia or other relevant materials.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top