Rebooting Functors: Why Functors-as-Containers Doesn’t Work and an Exotic Approach Just Might

If functors are the answer, what’s the programming question?

Is it “How do I model data in FP?”

That can’t be the whole story.

Parsers don’t fit the bill as containers. Neither do monads like STM, Par, and Async even though every one of them is a functor (as is every monad).

Here’s a better way of thinking about functors:

Functor = Function Space Makeover

You could also think of it as sexing up the space, but let’s keep this safe for work, shall we? ;)

A functor is a way of rocket-launching a bunch of nice-if-somewhat-boring [1] functions into a bigger ’n badder universe populated with exotica.

Man in the Mirror reveals biggest FP code smell: O Queen, so pure and fair thou art, ’tis true. But beware! Lest the impious get more play than you!

Functional Programming requires orchestrating a democracy of functions to work together. That means paying attention to the quirkiness of each inhabitant.

Different functors, different habitats, different breeds of exotics.

Confessions of the Made-Off Functor

… also known as Maybe or Option.

Functions behave like business transactions. I give you X, you give me Y. Pure functions are really good at that.

The Made-Off world drops the obligation to quid pro quo.

The pure ones are still around. But too bad for them, they gotta hang out with crazy folk that take values and give nothing back!

In a pure world you never have to rage, Y U NO ARR OH EYE?

Computers are normally quite pure. But things do break down. FP programs make use of the Made-Off functor (and its fancier cousins, Either and Error) to model exceptions when functions fail to deliver the goods.

Confessions of the List Functor

Let’s try another one.

The List functor is the epitome of data containers. What’s so exotic about it?

What are the aliens cavorting alongside “I give you X and you give me Y”?

Or “I give you one X and you give me the same number of Y”?

Because that’s what going on in List world: a distortion field operating on the meaning of “one”. And turning it into 0, 1, 2, and so on. With a linear ordering bolted on for kicks.

So whereas the pure ones now go about their lives in the new world on the basis of “I give you some number of X and you give me back the same number of Y in the same order”, exotics funk it up.

They could, like Made-Off, return nothing never. Or they could double the number or halve it. They could also mix up the order. Or they could do all of the above!

Which is why, modulo the ordering fanglement, the List functor is sometimes used to model non-determinism.

Want more? Vote on proggit for your favorite functor / monad. I’ll unpack the most popular ones in upcoming posts.


Notes:

  1. The functors found in programming are themselves a nicer subset of full-blown category theory. With one exception, programming functors are faithful. Which just means that we get an intact copy of the base space. The rocket launch doesn’t cause dents.

I like to make stuff that makes other stuff awesome.

Kim-Ee Yeoh @ky3atamo

p.s. Frustrated at not getting monads? Check out this course.