Skip to content

Instantly share code, notes, and snippets.

@RyanCCollins
Last active October 1, 2016 04:37
Show Gist options
  • Save RyanCCollins/9afc16b9170053e42b1bd723680ba205 to your computer and use it in GitHub Desktop.
Save RyanCCollins/9afc16b9170053e42b1bd723680ba205 to your computer and use it in GitHub Desktop.
Maybe and other functors JavaScript
var _Maybe = function(val) {
  this.val = val;
}

var map = function(f, obj) {
  return obj.map(f);
}

var Maybe = function(x) {
  return new _Maybe(x);
}

_Maybe.prototype.map = function(f) {
  return this.val ? Maybe(f(value)) : Maybe(null)
}

var endResult = Maybe(null).map(function(s) {
  return someFunction(s)
});

console.log(endResult);

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