Muzietto

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…

The state monad in Java 8, eventually…

In functional programming, java, monad on April 26, 2014 at 6:40 pm

This is no tutorial about the state monad and I’d be foolish if I hoped to use all these convoluted snippets of strongly-typed generics-laden Java code to show the monad’s beautiful essence to someone who hasn’t yet caught at least a glimpse of it elsewhere.

On the other hand, what I present here is a non-trivial case that in its original C# version has taught me a lot about handling real problems using this monad.

Keep reading.

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.

Follow

Get every new post delivered to your Inbox.