Beyond “Monads and Gonads”

In functional programming, functions, javascript, monad on October 31, 2014 at 6:51 pm

There’s this famous “Monads and Gonads” video from Doug Crockford. To me it is cryptic, kind of elitist: it flies over a lot of fairly complex stuff without giving sound grip on the matter. I can’t imagine anyone ‘seeing’ monads for the first time thanks to this presentation.

The major merits of this video are to state clearly that monads don’t need a type system, that monads ought to be objects taking care of their own adherence to monad laws and to show that JS is perfectly suited to manage those objects using a terse, straightforward syntax.

Besides, imho the presentation cuts too many corners and stops short of presenting and explaining meaningful examples. This led me to fork the original Crockford’s code and produce further applications of the monads presented inside it, as well as of new types.

Details inside…

State monad goes to JS town (and starts swinging…)

In functional programming, functions, java, javascript, monad on June 2, 2014 at 7:29 am

Javascript is the only language I am aware of that allows to invoke a monadic function, like state is, simply by calling an instance by its name and sticking a parameter between two round brackets: myStateMonad(aState) will return a state-content pair that I chose to represent as {state:aState,value:aValue}, but that could as well piggyback on arrays and read like [aState,aValue].

And because you have a monad instance equipped with all the required monad stuff, you may simply bind it to a custom-built function, and you get another monad instance. Then, keep chaining…

This is what I call intuitive. These concepts read easier in JS than in most other languages.

Too bad Javascript doesn’t have do-notation: that would end whatever discussion about the best language for learning FP :-)

Go on, read the whole post…

Fold Right in Javascript (with all thinkable examples)

In functional programming, javascript on September 11, 2013 at 8:44 am

Fold right (aka reduce right) is the basic list recursion operator. Way more intelligent than its cousin fold left. It has a non-intuitive way to handle its own parameters (trying to follow the nested calls with paper and pen or with the stepper of DrRacket is kind of funny), but the operation you entrust it with (represented by a function usually called f) gets performed outside the recursion call, which is the general thing you’d expect a recursive function to do. This makes fold right extremely versatile. Unlike fold left (which has signed a suicidal pact to reach the end of the list or blow the stack in the effort) during the trip with fold right you may talk to the driver and tell him for example to fetch the result and get out of the loop.

Keep reading.


Get every new post delivered to your Inbox.