For people who wish they didn't have to programme in JavaScript. Full documentation at http://cwmyers.github.com/monet.js/
Monet is a library designed to bring great power to your JavaScript programming. It is a tool bag that assists Functional Programming by providing a rich set of Monads and other useful functions.
This library is inspired by those that have come before, especially the FunctionalJava and Scalaz projects.
While functional programming may be alien to you, this library is a simple way to introduce monads and pure functional programming into your daily practises.
The source is available at: http://github.com/cwmyers/monet.js.
Simply download and add to your html pages or we also support bower. You can also include monet-pimp.js
which contains extra functions on the Object.prototype
for creating monads.
<script type="text/javascript" src="monet.js"></script>
<!-- Optionally -->
<script type="text/javascript" src="monet-pimp.js"></script>
bower install monet --save
# or to install a specific version
bower install monet#0.9.0-alpha.2
npm install monet --save
# or to install a specific version
npm install monet@0.9.0-alpha.2
As you know JavaScript isn't a strongly typed language. This kinda sucks. Types are a great help when it comes to functional programming as it makes the code more comprehensible and prevents a range of errors from being introduced.
Knowing the types of your functions and data is also important when writing documentation (such as this one), so we will invent some type annotations to make things more clear. We will only do this in the function definition and not in the concrete examples.
JavaScript doesn't have generic types but it's useful to know about them when dealing with Monads. For instance the List
monad is a type that requires another type, such as a string or integer or some other type before it can be constructed. So you would have a List of Strings or a List of Integers or generically a List of A
s where A
is a type you will supply. Now of course this is JavaScript and you can do as you please even though it doesn't make sense. But to make things clearer (hopefully) we will attempt to do show generics or type parameters thusly:
List[A]
Which means a List
of A
s. Though of course you will have to keep track of the types yourself.
function x(a: A, b: B): C
And functions on a Monadic type that has been constructed with A
Maybe[A].fromNull(a: A): Maybe[A]
For functions that take other functions as parameters (which are called Higher order functions) we will use an abbreviated way to represent that function using a pseudo type lambda:
A -> B
So,
function x(a: A -> B, c: B -> C): C
means that function x
takes two parameters that are both functions themselves. a
is a function that takes a type A
and returns a type B
and c
is a function that takes a type B
and returns a type C
. The function x
will return a type C
.
Some functions (or lambdas) do not take a parameter, and some do not return anything. Will express this as:
() -> A
or
A -> ()
Everything that is a monad in will implement the following functions. The specific monads will be discussed in detail below.
Monad[A].bind(f: A -> Monad[B]): Monad[B]
Performs a monadic bind.