Functor
- A
Functor
is any data type that defines howfmap
applies 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.
Applicative
pushesFunctor
aside.Control.Applicative
defines<*>
- 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. Monad
is another typeclass.Maybe
is aFunctor
, anApplicative
, and aMonad
- Haskell also provides us with some syntactical sugar for monads, called
do
notation
Conclusion
Functor
,Applicative
,Monad
are typeclass, the value implements them is oneFunctor
orApplicative
orMonad
- A functor is a data type that implements the
Functor
typeclass. - An applicative is a data type that implements the
Applicative
typeclass. - A monad is a data type that implements the
Monad
typeclass. - A
Maybe
implements all three, so it is a functor, an applicative, and a monad. - syntax sugar
- functors: you apply a function to a wrapped value using
fmap
or<$>
- 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: <a>
f <a> -> <b>
# applicative: <b>
<f> <a> -> <b>
# monad: f a -> <b>
<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
: likeApplicative
but I can decide what to do next after each step.
所以可以把这个 container 看作“更多数量的元素”,把monad看做可中断的流水线。
Hi! I am a robot. I just upvoted you! I found similar content that readers might be interested in:
http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html
Congratulations @weaming! You have completed the following achievement on Steemit and have been rewarded with new badge(s) :
Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word
STOP
Do not miss the last post from @steemitboard:
SteemitBoard and the Veterans on Steemit - The First Community Badge.
Congratulations @weaming! You received a personal award!
You can view your badges on your Steem Board and compare to others on the Steem Ranking
Do not miss the last post from @steemitboard:
Vote for @Steemitboard as a witness to get one more award and increased upvotes!