λ Tony's Blog λ
Functors and things using ScalaPosted on March 1, 2013
Types of Functors
There are many types of functors. They can be expressed using the Scala programming language.
- covariant functors — defines the operation commonly known as
- contravariant functors — defines the operation commonly known as
- exponential functors — defines the operation commonly known as
xmap. Also known as invariant functors.
- applicative functor1 — defines the operation commonly known as
- monad2 — defines the operation commonly known as
- comonad3 — defines the operation commonly known as
Remembering the different types
Sometimes I am asked how to remember all of these and/or determine which is appropriate. There are many answers to this question, but there is a common feature of all of these different types of functor:
They all take an argument that is some arrangement of three type variables and then return a function with the type F[A] => F[B].
I memorise the table that is the type of the different argument arrangements to help me to determine which abstraction might be appropriate. Of course, I use other methods, but this particular technique is elegant and short. Here is that table:
We can see from this table that there is not much reason to emphasise one over the other. For example, monads get lots of attention and associated stigma, but it’s undeserved. It’s rather boring when put in the context of a bigger picture. It’s just a different arrangement of its argument (
A => F[B]).
Anyway, this table is a good way to keep a check on the different types of abstraction and how they might apply. There are also ways of deriving some from others, but that’s for another rainy day. That’s all, hope it helps!