Category Theory (Functional Style) part-1
Tags: javascript, typescript, functional, programming
Published: 2024-02-24 12:01:11


# Category Theory 

Category theory is a way to abstract programming, enabling the creation of complex programs that are readable, extensible, and testable. It provides a mathematical abstraction for code.


#### category consist of type object and morphs
- morphs interpreted as a function or operation that transforms data from one form to another.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o8ixnsfmmkhzvrvl30df.png)


- object in functional programming might refer to a data structure that bundles together related information.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2421uxk46xj8qd8m9d8g.png)


`f:a -> b // f is morphs and a is object`

 morphs might refer to functions or transformations that take one input and produce a modified output without any internal state or side effects these types are known as pure functions. 

# What is Pure functions

Pure functions are deterministic, meaning they always produce the same output given the same input, and they don't rely on or modify any external state.

# what is Composition 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4e86x91nt421v3mzcq9p.png)


To form Relation between two morphs. 

The practice of combining simpler functions to build more complex functions. It involves taking two or more functions and creating a new function by chaining them together, where the output of one function becomes the input of the next function.
for example in js/ts - map,filter,reducer

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/olnvk3ljsod04s4g0q4t.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cpdt4wna6r2esk504t73.png)





```
f:a->f:b->c
composition - g∘f
```

Functional Style
# Rules of Category 

1> Composition definition 
2> Composition associative
3> Composition Identity



#### Composition Definition 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4kr5bqxvrtlhqcib46me.png)


If you have two functions composed one after another, it implies that there exists a third arrow starting from the first function and ending at the second function.

f:A -> B and g:B -> C then k = g * f,
where k:A -> C

composition k is defined as mapping elements from A to C by first applying function f to map elements from A to B and then applying function g to map the result from B to C.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d4n1dp7r42zycj9d43yo.png)

#### Composition Associative
The order of the Composing function should not matter. Whether you first compose f and g  than h or g and h and then f. 
  
f: A -> B, g:B -> C and h:C -> D then,
(f * g) * h === f * (g * h)

it assures us that no matter which order we apply the compositions than final result will be the same.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lmrube4mxoqgquzl76bj.png)



#### Composition Identity 
f:A -> A


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9feahthrwrywi2jl84gl.png)



 It is a function that maps every element of a set 
A to itself.