🍫 CocoaPatterns — iOS & Mac Development Since 2009 RSS
🍫 CocoaPatterns
Because someone has to write this stuff down
"We ship apps. Sometimes they even work."

How I Forgot Everything I Knew About Programming and Relearned It All Using Haskell and Monads

A journey of six months, one job, two relationships, and approximately 400 hours of reading papers I didn't understand.

TW
Thaddeus Wren
@twren_fp · CocoaPatterns

Six months ago I was a happy, employed iOS developer. I had a job. I had a girlfriend. I had a 401k that was performing adequately. Then I read a blog post titled "Haskell Will Change the Way You Think About Code Forever," and I thought: great, I'd like my thinking changed. I did not appreciate how thorough the change would be.

"A monad is just a monoid in the category of endofunctors. What's the problem?" — A person who lost their job

Month One: Innocent Curiosity

The first month was fine. Maybe types. Pattern matching. Pure functions. I was smug at standup. "Well, in a pure functional language, this side effect wouldn't even be possible." My coworkers smiled patiently. I was insufferable and I did not know it.

Month Two: The Monad Revelation

Month two is when I hit monads, and month two is when things went sideways. I read seventeen different "monads explained" blog posts. Every single one used a different metaphor. Burritos. Boxes. Astronauts. Railway tracks. A man in a trench coat who is actually three men. I understood none of them and I understood all of them and I was not sure those were different states.

📖 My Reading List

In month two alone I read: "Learn You a Haskell," "Real World Haskell," four category theory primers, a dissertation on applicative functors, and a Reddit thread that made me feel like I needed a PhD before I was allowed to write a web server.

Month Three: The Turning Point

I rewrote our iOS networking layer in a "monadic style." In Objective-C. Using macros. The result was 2,000 lines of code that bound Maybe-like result objects through a chain of block-based transforms. It compiled. It worked. No one else on the team could read it, including, after two weeks, me.

// Month 3 Brad wrote this. Month 5 Brad found it and wept.
return [[[[[MaybeM(fetchUser(userId))
    bind:^(User *u){ return MaybeM(fetchProfile(u.profileId)); }]
    bind:^(Profile *p){ return MaybeM(validateProfile(p)); }]
    bind:^(Profile *p){ return MaybeM(enrichProfile(p)); }]
    bind:^(Profile *p){ return MaybeM(renderProfile(p)); }]
    valueOrDefault:nil];

The Aftermath

I am back to writing normal Objective-C. My girlfriend came back after I stopped explaining monads to her at dinner. The networking code was quietly replaced by a junior engineer who used NSURLSession and completion blocks "like a normal person." She got promoted. I was not promoted. I did learn what a monad is, though I could not explain it to you. That's where we are.

3 Comments — Powered by Disqus
KC
kchang_swift · 1 week ago
"Month 5 Brad found it and wept." This is my autobiography.
TW
twren_fpAuthor · 6 days ago
The networking code is still partially in production. There's one endpoint nobody will touch. We call it The Monad. It has 100% uptime. No one knows why.
RS
rishi_s_codes · 5 days ago
"A man in a trench coat who is actually three men" describes every monad tutorial I've ever read.