I'm trying to understand the relationship between a functor and a monoid. They are often mentioned together but I haven't been able to quite connect the dots.
I understand that, simply speaking, in programming a monoid can be thought of as a structure or data type with an associative append/concat function used to combine elements in the structure as well as an identity element where if you commutatively combine the identity value with an element in your structure it will always return the same element.
I also recognize that in programming a functor can be thought of as a collection-like structure with a map operation similar to Array.prototype.map()
.
Could anyone help me see the big picture here? Also, please feel free to let me know if I'm missing anything in my understanding of these concepts.
Both of these terms came from category theory a Math branch that study the cathegories of elements, the relations among them and the composability of functions between the elements. This discipline has strong influence in functional programming and because of that usually these terms appear in discussions about these paradigm.
In practical terms the translation of functor and monoid terms into programming is the following:
Functor preserves the structure and relations between elements of two different cathegories, this means that a Functor is a "structure" that provides a constructor of one element.(preserves the structure because each element is mapped into a element of the other cathegory) and a map function (preserves the relations "functions" mapping each function of the original cathegory into the target one)
Monoid it is an endofunctor (functor that origin and target cathegory is the same) that defines and identiy operation and associative operation for example a list is a monoid because it defines an identity operation (empty list) and associative operation (append)