I recently learned what monads are, and predictably, had just as much trouble "getting it" as everyone else. But they're not actually cursed; it's an extremely simple concept, it's just that everyone either 1) Tries to explain the mathematical background, which is not necessary (You don't need to know anything about set theory to add two numbers!), 2) Uses a bunch of Haskell code which is gibberish to imperative/OOP programmers, or 3) Starts with a box metaphor, which is just in medias res non-sequitur: you have to explain why the boxes are there first!
Long story short, I'm pretty sure I can break the curse. Here's my monad tutorial:
I ended up learning about monads by writing them as a useful design pattern, then being told that's what they were. Amusingly enough, I picked up that pattern from writing a lot of Common Lisp. I also tended to explain it to people using the Maybe example, and the Error Wrapper example (Either in your guide), so it's nice to see that played out with all the basic explanations in place too.
The only thing I might recommend doing is bolding "entangle" the first time you use it in the paper, and/or bold "entagled values" when that occurs slightly later. I tend to skim a bit when text starts getting really thick, so having key ideas like that bolded helps readability for people like me.
I'm only loosely aware of monads and in theory know what they are. Like I knew that a JS Promise is a monad-like structure, but not exactly as to why.
I'd say I understand Monads a bit better from an imperative context, but I also think your tutorial isn't necessarily the easiest to understand. I think if one has a lot of programming experience, one can follow along, but at the very end of the day monads are complex, and must take a complex chain of thought to understand. And, if anything, actually making a monad and playing around with them interactively is the best way to learn.
But as for me, I will definitely say I came out learning a lot more about them. So, I do think it's effective for more seasoned programmers, Does it break the curse? I dunno, but I'm sure you'll find out someday!
Well, I mean, I do expect people reading a monad tutorial
to have existing programming background, that goes without saying. I will say that I think imperative programmers are better equipped to understand them than functional programmers actually, since we're
the ones writing all the boilerplate in the first place. Think of your Link.js
library and the problem it solves :)
Programmer-to-programmer: the thing is, the monad concept itself is not actually complicated, people just make it seem that way - sure, individual monad types are complex (promises, e.g.), but the monad interface
is ridiculously simple:
let arr = Array.of(1, 2, 3);
let m = new Maybe("foo");
let prom = Promise.resolve(812);
arr = arr.map(x => x * 2); // we know what this does
m = m.map(s => s + "bar"); // maybe it has a value, maybe not - it's a no-op if not
prom = prom.then(value => newValue); // just map to new value, no async shenanigans
prom.then(result => new Promise(...)); // promise chaining
m.thru(value => new Maybe(result)); /* alternatively, Maybe.Empty */); // maybe it can fail
arr.flatMap(elem => Array.of(...)); // one-to-many mapping
That's it. If you implement that interface and satisfy the identity laws, it's a monad. Literally. That's all that's required. It doesn't matter what kind of abstraction it's an interface *to* (promises, arrays, maybes, eaty pigs...), you just need to implement the interface. The main thing is that the pattern
this interface represents already exists in "nature"--you have to train yourself to recognize it though, and that's
the actual hard part.
Thanks for the tutorial. I reciprocate by giving you a link to Uncle Bob. I love his perspective about "new" programming paradigms. Don't want to give too much away, here it is:
And yeah, as a dinosaur, he knows Monads too and has a clojure perspective:
Here are the slides of the presentation:
And... ah, here it is, the presentation itself:
don't fall asleep now ;) (yeah, they are long but fun, for a certain type of programmers fun)
@FatCerberus: I think you just nailed it with that post, bud. :) That's indeed the simplest explanation yet. I was looking for a simple overview, like an "all-together", and what you just wrote, is it.