Functor is an algebraic structure.

Laws

  1. If u is a functor, then calling u.map(x => x) must be equivalent to u. This is the identity law.
  2. If u is a functor, and f and g are functions, then calling u.map(x => f(g(x))) is equivalent to calling u.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 a and transforms it into something of type b. The types a and b can be anything—even the same type.
  • And when you call .map() on a functor of a, you’ll get back a functor of b.

OCaml

A “function” from modules to modules

module F (M : S) = ...
end