## Roy - Monad Syntax

2011-06-19

— I added monad syntax to Roy. It is very close to do-notation, with the exception that the monad implementation is explicit:

```
data Option a =
Some a | None
let optionMonad = {
return: fn x =
Some x
bind: fn x f = match x
case (Some a) = f a
case None = None
}
console.log (do optionMonad
bind x = Some 1
let y = 2
bind z = Some 3
return x + y + z
)
```

Note the `do optionMonad`

. Explicitness is only necessary because Roy doesn't have any form of interfaces or type classes. An upside is that a single datatype could have multiple monad implementations, something not allowed in most other languages.

I also worked on prettying up the JS output. It now looks like this:

```
"use strict";
var Some = function(a){this._0 = a};
var None = function(){};
var optionMonad = {
"return": function(x) {
return new Some(x);
},
"bind": function(x, f) {
return (function() {
if(x instanceof Some) {
var a = x._0;
return f(a)
} else if(x instanceof None) {
return new None()
}
})();
}
};
console.log((function(){
var __monad__ = optionMonad;
return __monad__["bind"](new Some(1), function(x) {
var y = 2;
return __monad__["bind"](new Some(3), function(z) {
return __monad__["return"](x + y + z);
});
});
})())
```

Please enable JavaScript to view the comments powered by Disqus.