Skip to content

Functor

Spicy — senior dev territoryGeneral Dev

ELI5 — The Vibe Check

A functor is a box you can map over. Array? Functor. Optional? Functor. Promise? Functor. If you can apply a function to what's inside without unwrapping the box, congratulations — you've been using functors all along.

Real Talk

A type F that supports a 'map' operation: given F and a function T -> U, produce F. Functors preserve structure — mapping over an array gives an array, mapping over an Option gives an Option. They must obey identity and composition laws. Arrays, Promises, and Options are all functors.

Show Me The Code

// Array is a functor: .map() applies a function inside the 'box'
[1, 2, 3].map(x => x * 2); // [2, 4, 6]

// Option/Maybe is a functor
const name = user?.profile?.name; // mapping through optional chain

When You'll Hear This

"Every time you call .map() on an array, you're using it as a functor." / "Functors are simpler than monads — if you understand Array.map, you understand functors."

Made with passive-aggressive love by manoga.digital. Powered by Claude.