At work I get to ask Adam Beck noob questions about monads.
So, Functors
How are functors relevant to monads?
Functors are a type class. A generic if you're familiar with .NET/Java type systems.
Okay, so the <T>
stuff?
Yup. Functor<T>
. And T
can be anything.
How are functors related to functions?
They are not directly related to a function. They are just things that respond to a map
function.
So like an OOP class but for Types?
It's not an OO class. Functor
is just ML's way of saying generic
.
But it is like OOP class → object, Type class → type?
Correct.
So it is a way to group types together?
Correct.
Summary so far:
Functors are a type class.
→ Functors are a class of types.
Functors respond to the map
function
Always?
Always and forever.
That's part of their type definition.
Summary so far:
Functors are a class of types that respond to amap
function
Right.
The map
function has the signature of
map<TIn, TOut>( ƒ(x:TIn):TOut ): Functor<TOut>
So are there other "type classes" that do not respond to a map
function?
Yeah. A List
might not respond to a map
but it needs to respond to forEach
.
Again, a type class is any generic.
So a List
is not a functor
?
It does not have to be, technically.
Summary so far:
Functors are a class of types that respond to amap
function.
There are different classes of types out there.
Lists are a class of types that respond toforEach
.
When is a List
a functor
? When it responds to map
?
When it responds to map
- correct.
Got it.
The map
function has to follow the signature of
map<TIn, TOut>( ƒ(x:TIn):TOut ): Functor<TOut>
What that looks like in practice is (using javascript)
let functor = Functor.of(1)
functor = functor.map((one) => one + 2) // Functor<3>
The of
function is part of the pointed functor type class.
A pointed functor has both of
and map
.
It simply creates a functor with the provided value, in this case 1
.
Summary so far:
Functors are a class of types that respond to amap
function.
There are different classes of types out there.
Lists are a class of types that respond toforEach
.
There exists a thing called a pointed functor.
To be continued…