January 24th, 2014


My first experience with Monadism

[Yes, I know, it sounds sordid. I couldn't resist, because even programming virginities are intimidating the first time. The truth is, this is a deeply geeky programming post, for the serious engineers. This is a fairly long burble -- I encourage the curious to ask questions. The experienced functional programmers will mostly just nod sagely, recognizing a newbie getting past a hurdle, but those who are still trying to get a handle on Monads may be interested in how I got through it.

NB: this is getting into fairly advanced Scala -- levels A3/L2 in the standard rankings. Note that you don't need this sort of stuff to *use* Scala most of the time, just if you want to write really powerful libraries and frameworks. I'm painfully aware that posts like this tend to lead to the common "But Scala is *hard*" whinging online, and it isn't actually true. Scala is *powerful*, and using that power to its fullest *is* a bit hard. But if you just want to write routine application code, it's as easy or easier than Java. This post is basically an example of how an Architect *makes* it that easy for the application engineers.]

Querki is chock-full of Functions -- that is, the code defines a lot of Functions that users can use in QL to transform their data. There are dozens of them now; there will be hundreds in the medium term, and it wouldn't astonish me if that eventually climbs into the thousands as we add more specialized App Libraries to mix in.

To date, writing those Functions has been laborious, and rather boilerplate-filled, which has irked me for a long time. I realized quite some time ago, in a vague way, that this was the sort of problem where Monads are supposed to help, but for a very long time I didn't grok them well enough to make that meaningful. But today I crossed the Rubicon, and wrote my first Monad. To my great satisfaction, it worked correctly right off the bat. So I think I'm finally starting to get it.
Collapse )