Functor is an algebraic structure.
Laws
- If
u
is a functor, then callingu.map(x => x)
must be equivalent tou
. This is the identity law. - If
u
is a functor, andf
andg
are functions, then callingu.map(x => f(g(x)))
is equivalent to callingu.map(g).map(f)
. This is the composition law.
Examples
TypeScript
- The
.map()
method takes a function as an argument. - That function must take something of type
a
and transforms it into something of typeb
. The typesa
andb
can be anything—even the same type. - And when you call
.map()
on a functor ofa
, you’ll get back a functor ofb
.
OCaml
A “function” from modules to modules