JavaScript example of an (endo)functor that is not the identity functor

or Functors in cathegory theory vs in functional programming

Attila Gulyas
Jul 23, 2017 · 4 min read

(The examples and most of the definition itself is taken from Jared Smith’s answer on Stack Overflow but I would also like to thank the commenters` contributions!)

A functor

  1. is a data structure
    (JavaScript — closure, Array, Object etc.,
    Haskell — types created with data, type, newtype)
  2. supports a mapping operation
    (e.g., the map function in Arrays in JavaScript or
    the fmap in Haskell’s Data.Functor)
  3. respects identity ( xs.map(x => x) === xs )
  4. respects composition ( xs.map(f).map(g) === xs.map(compose(f,g) )

The identity functor is an endofunctor (as almost any other functor in functional programming (links))<-

Arrays or an Object that conforms to functor requirements in functional programming fit the description in the title and also that of a functor.

Uncategorized resources that I found helpful

I came across Professor Frisby Introduces Composable Functional JavaScriptwhen I was trying to find resources to tackle functional programming terms that I couldn’t understand from reading PureScript by Example ¹.

I have a bad habit (that pays off occasionally) that when reading about something new to me, I tend to obsess on the first new concept that I can’t understand, instead of reading (or skimming) through the resource, noting my knowledge gaps and re-read it.

The very first lesson mentioned the identity functor so I started googling what a functor is. According to Wikipedia:

In mathematics, a functor is a type of mapping between categories arising in category theory. Functors can be thought of as homomorphisms between categories. In the category of small categories, functors can be thought of more generally as morphisms.

The only new infomation that this gave me was that I had no idea what homomorphisms (and morphisms in general) are.

Homomorphisms

Google’s definition of homomorphism is general but it puts things into context:

a transformation of one set into another that preserves in the second set the relations between elements of the first.

Wikipedia says that it

is a structure-preserving map between two algebraic structures of the same type (such as two groups, two rings, or two vector spaces).

(…)

The concept of homomorphism has been generalized, under the name of morphism, to many other structures that either do not have an underlying set, or are not algebraic. This generalization is the starting point of category theory.

Onward to morphisms.

Morphisms

According to Wikipedia

morphism refers to a structure-preserving map from one mathematical structure to another.

Almost the same as homomorphisms but more general. It continues to say that

In category theory, morphism is a broadly similar idea, but somewhat more abstract: the mathematical objects involved need not be sets, and the relationship between them may be something more general than a map.

The study of morphisms and of the structures (called “objects”) over which they are defined is central to category theory. Much of the terminology of morphisms, as well as the intuition underlying them, comes from concrete categories, where the objects are simply sets with some additional structure, and morphisms are structure-preserving functions. In category theory, morphisms are sometimes also called arrows.

The very last sentence helped me a lot because I found this expression in Haskell and other functional programming tutorials as well. So, just to remember it:

morphism === arrow

The part that I missed on the first run was what a category is and a very simplistic view of them according to the article is

A category C consists of two classes, one of objects and the other of morphisms.

There are two objects that are associated to every morphism, the source and the target.

For many common categories, objects are sets (usually with more structure) and morphisms are functions from an object to another object. Therefore, the source and the target of a morphism are often called domain and codomainrespectively.

Identity morphism vs identity functor


  1. ^ Phil Freeman’s introduction to PureScript is excellent but I had a hard time following it because I had to stop after every sentence. It was obvious that I needed to bone up on the basics.
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade