# Category Theory and Programming

Category theory is providing the abstract of the mathematics. Category theory formalize mathematics structure. Category theory is used to develop common language between mathematics.

In category theory, object and arrows (or morphisms) between objects. For each object there is a identity morphism and object arrows/functions/morphisms can be composed in associative fashion

For example: A — f() –> B — g() –> C then A — f().g() –> C here A, B, C are objects and strange arrays are morphims. This is showing that the object A when given morphism gives B.

In real world these can be set of Animal such as Cat, Dog, Elephants and when function is provided they gives there classes such as Carnivorous

Category theory is associative and identity. (h.f).g = h.(f.g) and f().1a = A

Category theory emphasis more on morphism. There are different types of morphisms. I had not deep dive into type of morphisms in category theory however when learning category theory with programming, I came across functors and monad most.

Functor is the structure preserving morphism often called as homophorism. These are the mapping between two categories.

In category theory, Monad and Monoid are same. They are also called endofunctor. Endofunctor are the category object morphism back to same category. The good real world example can be clock. Monad and Monoid in category theory are same however, they are different in programming because they operate at different level of abstraction.

Functor

Functor in programming implements a function called map.

Example of functor in Scala is List[A] => List[B] or more concretely List[Int] => List[String]

``List(1, 2, 3).map((x: Int) => x.toString) ``
``````trait Functor[F[_]] {
def map[A, B](f: A => B): F[B]
}``````

This category in programming help programmer to write code in more abstract way.

``````trait Monad[T]