liveBook · Manning

Functor

As explained in Fabulous Adventures in Data Structures and Algorithms

A functor is a mapping between categories that preserves their objects, morphisms, and composition.

A functor maps the objects of one category to another while preserving morphisms and their composition. It transfers structure rather than merely transforming values. A covariant functor preserves arrow direction; a contravariant functor reverses it. When both categories are the same, the functor is an endofunctor. section 7.4

Read a functor through three connected ideas:

  • Treat objects as arbitrary entities and morphisms as arrows expressing how one object relates to or reaches another. section 7.1
  • Use covariance when relationships retain direction, and contravariance when the same relationships reverse direction. section 7.4
  • Interpret assignment compatibility between types as a morphism; generic interfaces can then behave like covariant or contravariant functors. section 7.4

Mapping a small category

A C# function such as `string F(int i) => family[i]` maps integer objects to names. Thinking categorically adds a further requirement: the mapping must preserve the relevant arrows between those objects, not just produce an output for each input. section 7.1

Figure 7.3 The function F is represented as dashed arrows mapping from one category to another.

A diagram makes the functor visible as dashed arrows mapping one category's objects into another. section 7.1

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Defines categories, morphisms, variance, and endofunctors, then stops short of deeper category-theoretic development.
Examples
Uses a three-object category, a C# integer-to-string function, and a diagram of dashed mapping arrows.
What it assumes
Builds categories and morphisms from basic definitions while assuming comfort with functions, types, and programming abstractions.
What you can do after
Evaluate type relationships and generic interfaces using covariance, contravariance, and preserved morphisms.
How it connects
Connects functors to categories, morphisms, covariance, contravariance, endofunctors, type systems, and generic interfaces.

Also explained in

Compare how 6 books explain functor →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fabulous Adventures in Data Structures and Algorithms. The section links point to the parts of the book it draws on.