Muzietto

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.

Handling I/O with the state monad in Javascript

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

The state monad allows to chain (i.e. to execute in sequence) a wide sort of operations, each one returning one single parameter called “value”. A second parameter called “state” is passed through the chain of the operations, acting as a representation of the evolving state of the system throughout the various steps. A JS implementation of the state monad is presented here. The state monad appears to be very well capable of handling I/O operations. A simple GUI composed by prompt (returning as value the user input) and alert (returning as value undefined) is put at work with interesting results. All this in the comfort of your favorite browser.

Read the whole post.

Chainable monads in JavaScript

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

The chainable monads presented here are Javascript functions that allow idiomatic expressions such as

monad1.bind(function1) —> monad2

Because Javascript is typeless, there’s not really any reason to talk about monad[A] rather than monad[B], but the raison d’être of monads remains. Javascript monads are created and manipulated by running  convenient functions. The first monad in the chain usually comes from the run of a ‘unit’ function. This article shows all this in the case of the clueless Maybe monad. For more professionally relevant talk, you may want to go asap to the next article.

Read the whole post.

Follow

Get every new post delivered to your Inbox.