Functor
- A
Functoris any data type that defines howfmapapplies to it. - Lists are functors too!
- Functions are Functors too. When you use fmap on a function, you're just doing function composition!
- Functors apply a function to a wrapped value
Applicatives
- Applicatives take it to the next level. With an applicative, our values are wrapped in a context.
ApplicativepushesFunctoraside.Control.Applicativedefines<*>- Applicatives apply a wrapped function to a wrapped value
Monads
- Monads apply a function that returns a wrapped value to a wrapped value.
- Monads have a function
>>=(pronounced "bind") to do this. Monadis another typeclass.Maybeis aFunctor, anApplicative, and aMonad- Haskell also provides us with some syntactical sugar for monads, called
donotation
Conclusion
Functor,Applicative,Monadare typeclass, the value implements them is oneFunctororApplicativeorMonad- A functor is a data type that implements the
Functortypeclass. - An applicative is a data type that implements the
Applicativetypeclass. - A monad is a data type that implements the
Monadtypeclass. - A
Maybeimplements all three, so it is a functor, an applicative, and a monad. - syntax sugar
- functors: you apply a function to a wrapped value using
fmapor<$> - applicatives: you apply a wrapped function to a wrapped value using
<*>orliftA - monads: you apply a function that returns a wrapped value, to a wrapped value using
>>=orliftM
- functors: you apply a function to a wrapped value using
# normal
f a -> b
# functor:
f <a> -> <b>
# applicative:
<f> <a> -> <b>
# monad: f a ->
<a> (f a -> <b>) -> <b>
所以
- functor 解包参数
- applicative 解包函数
- monad 把一个既不是normal也不是functor情形的函数和一个包裹参数颠倒顺序
另外一种视角
Functor: apply a function to a container.Applicative: apply a multi-argument function to multiple containers.Monad: likeApplicativebut I can decide what to do next after each step.
所以可以把这个 container 看作“更多数量的元素”,把monad看做可中断的流水线。