While functional programming may be alien to you, this library is a simple way to introduce monads and pure functional programming into your daily practices.
The source code is available at github.com/monet/monet.js.
Full detailed documentation can be found here
npm install monet --save # or to install a specific version npm install firstname.lastname@example.org
Simply download and add to your html pages. You can also include
monet-pimp.js which contains extra functions on the
Object.prototype for creating monads.
<script src="monet.js"></script> <!-- Optionally --> <script src="monet-pimp.js"></script>
Maybe type is the most common way of representing nothingness (or the
null type) with making the possibilities of
NullPointer issues disappear.
Maybe is effectively abstract and has two concrete subtypes:
Either (or the disjunct union) is a type that can either hold a value of type
A or a value of type
B but never at the same time. Typically it is used to represent computations that can fail with an error. Think of it as a better way to handle exceptions. We think of an
Either as having two sides, the success is held on the right and the failure on the left. This is a right biased either which means that
bind) will operate on the right side of the either.
Validation is not quite a monad as it doesn’t quite follow the monad rules, even though it has the monad methods. It that can hold either a success value or a failure value (i.e. an error message or some other failure object) and has methods for accumulating errors. We will represent a Validation like this:
E represents the error type and
A represents the success type.
An immutable list is a list that has a head element and a tail. A tail is another list. The empty list is represented by the
Nil constructor. An immutable list is also known as a “cons” list. Whenever an element is added to the list a new list is created which is essentially a new head with a pointer to the existing list.
Much like the immutable list, a Non Empty List can never be empty. It implements the
comonad pattern. It has a guaranteed head (total)
and a guaranteed (total) tail.
IO monad is for isolating effects to maintain referential transparency in your software. Essentially you create a description of your effects of which is performed as the last action in your programme. The IO is lazy and will not be evaluated until the
run) method is called.
Reader monad is a wonderful solution to inject dependencies into your functions. There are plenty of great resources to get your
teeth into the
Reader monad such as these great talks.
Reader monad provides a way to “weave” your configuration throughout your programme.