Meet the Applicative typeclass. It lies in the Control.Applicative module and it defines two methods,
pure and <*>. It doesn't provide a default implementation for any of them, so we have to define them both if we want something to be an applicative functor.
I am trying to understand who is using
pure function. I do use
(<*>) function for which applicative functors are most useful for. But I am not sure who really uses
I read something like
pure (+3) <*> Just 10 but it can be written as
Just (+3) <*> Just 10 too.
Above is just one confusion of too many I have. What is the true purpose of defining
pure and when do I get to use it (or) who is already using it?
<*> :: f (a -> b) -> f a -> f b, this operator accepts a function in the applicative type, as well as a value in the applicative type. The first argument of this operator can not thus be simply a function, but it has to be residing within an applicative.
pure function solves problems that might occur here (e.g wanting to apply a function that is not residing in the applicative). It accepts a function that is not currently residing in the applicative, and lifts it into the applicative.
pure :: a -> f a
(+3) :: Int -> Int, and
Just 10 :: Maybe Int, you can not thus evaluate
(+3) <*> Just 10 as the types do not work; the
(+3) has to be promoted to a value in the
Maybe a, the definition of
pure = Just, which is why you can write either
pure (+3) or
I'll leave it to you to look into the
<$> operator Remember, every Applicative is a Functor.