At work I get to ask Adam Beck noob questions about monads.
Functors are a type class. A generic if you're familiar with .NET/Java type systems.
<T>
stuff?Yup. Functor<T>
. And T
can be anything.
They are not directly related to a function. They are just things that respond to a map
function.
It's not an OO class. Functor
is just ML's way of saying generic
.
Correct.
Correct.
Summary so far:
Functors are a type class.
→ Functors are a class of types.
map
functionAlways and forever.
That's part of their type definition.
Summary so far:
Functors are a class of types that respond to amap
function
The map
function has the signature of
map<TIn, TOut>( ƒ(x:TIn):TOut ): Functor<TOut>
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.
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
.
List
a functor
? When it responds to map
?When it responds to map
- correct.
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...