liveBook · Manning

Endofunctor

As explained in Fabulous Adventures in Data Structures and Algorithms

An endofunctor is a functor that maps a category to itself.

An endofunctor maps every object in a category to another object in that same category while preserving morphisms. In the category of types, assignment compatibility supplies the morphisms. section 7.1 section 7.4

For a covariant generic interface such as IE<T>, the mapping sends each type T to IE<T>. If Giraffe is assignment-compatible with Animal, IE<Giraffe> must likewise be assignment-compatible with IE<Animal>. Covering every type requires allowing arbitrarily nested constructions such as IE<IE<Giraffe>>. section 7.2

Covariant generic interface

interface IE<out T> { T Current { get; } bool MoveNext(); } IE<Animal> values = giraffes; The out modifier makes the output-only type parameter covariant, permitting an IE<Giraffe> value to be assigned to IE<Animal>. The resulting type mapping mirrors the original assignment-compatibility arrows. section 7.2

interface IE<out T> 
{ 
    T Current { get; } 
    bool MoveNext(); 
}

Inspect the covariant interface declaration that makes the endofunctor analogy visible in C#. section 7.2

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Develops the definition through assignment compatibility and nested generic types, but only sketches the broader theory.
Examples
Uses C# types, assignment compatibility, IE<T>, and arbitrarily nested generic constructions as concrete examples.
What it assumes
Assumes generic interfaces and assignment compatibility, while defining categories, morphisms, functors, and endofunctors along the way.
What you can do after
Evaluate generic variance by interpreting type constructors as mappings that preserve assignment-compatibility relationships.
How it connects
Ties endofunctors to categories, morphisms, functors, covariance, contravariance, generic interfaces, and monads.

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.