Skip to content

Instantly share code, notes, and snippets.

@michaelficarra
Forked from pselle/monads.js
Last active August 29, 2015 14:07
Show Gist options
  • Save michaelficarra/cd66c547cbd0d4daa9e1 to your computer and use it in GitHub Desktop.
Save michaelficarra/cd66c547cbd0d4daa9e1 to your computer and use it in GitHub Desktop.
var _Container = function(val) { this.val = val }
var Container = function(val) { return new _Container(val); }
Container.prototype.map = function(f) {
return new Container(f(this.val));
}
Container.prototype.flatMap = function(f) {
return f(this.val);
}
var c = new Container(2);
// functor
c.map(function(x) { return x+2});
// monad
c.flatMap(function(x){ return x+2}).flatMap(function(x) {return x* -1 });
@DrBoolean
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment