Published
- 3 min read
Category Theory for People in a Hurry
The Call of the Void
Have you ever looked at your React code - specifically a useEffect hook that depends on a state variable that depends on a prop that depends on a context - and thought, “There must be a God, and He must hate us”?
I have, and that’s the only context you need for this post.
What is it? (Wrong Answers Only)
If you ask a Haskell programmer what a Monad is, they will say: “A monad is just a monoid in the category of endofunctors.” And then they will look at you with pity, as if you just asked how to use a fork. This sentence is true and useless to anyone who hasn’t already spent three months staring at commutative diagrams.
Category Theory is often called “The Mathematics of Mathematics.” It studies relationships (ohhh, so like psychology!). It studies structure itself, where the things being related don’t matter.
Beauty of Uselessness
Why learn this? I’m a programmer, not a mathematician!!! Indeed, writing “Category Theory Style” code in a JavaScript team is a great way to get your PR rejected and your desk moved to the basement.
I learned it for the same reason people climb mountains or learn Latin: ego Because it is beautiful.
There is a certain beauty in seeing two completely unrelated fields of math like topology and algebra and realizing they are isomorphic. That the same structural laws govern the shape of a coffee mug and the solution to a polynomial equation.
The Side Effect
The dangerous thing about Category Theory is that once you see the world in terms of composition, morphisms, and functors, you can’t un-see it. You will see Kleisli composition laws being violated everywhere. You become annoying at parties.
But you also gain clarity.
- Dependency Injection? That’s just Reader Monad.
- Asynchronous Events? Promises/Futures (which are… well, monad-ish). We are reinventing the wheel, but our wheels are square, made of mud, and we call them “Best Practices.”
A Burrito, A Space suit, A Box
Let’s talk about the Monad.
Imperative programming is like a recipe.
- Break eggs.
- Heat pan.
- Cook eggs.
- Eat.
If step 2 fails (no gas), step 3 crashes.
Binding a Monad is like building a pipe. You connect the “Break Eggs” pipe to the “Heat Pan” pipe. The connection itself handles the context. If “Break Eggs” yields “No Eggs,” the pipe doesn’t crash; it just passes “No Output” down the line. The rest of the factory keeps running, knowingly processing nothing.
Why it Matters
You will probably never write Haskell in production (I hope). You will likely stay in the side-effect-heavy world of JavaScript or Python.
But learning Category Theory builds character :P
The Ivory Tower is Nice
I admit it to be true that I am an ivory tower elitist when it comes to this stuff. I love the abstractness. It is a form of escapism, away from the real, messy world of software engineering.
And sometimes, I wonder if my javascript-weary computer dreams of electric sheaves.