Functor is an algebraic structure.
Laws
- If
uis a functor, then callingu.map(x => x)must be equivalent tou. This is the identity law. - If
uis a functor, andfandgare functions, then callingu.map(x => f(g(x)))is equivalent to callingu.map(g).map(f). This is the composition law.
Examples
TypeScript
interface Functor<A> {
map<B>(f: (a: A) => B): Functor<B>;
}- The
.map()method takes a function as an argument. - That function must take something of type
aand transforms it into something of typeb. The typesaandbcan 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
module F (M : S) = ...
end