(function(global) { var types = function(f, obj) { throw new TypeError("fmap called on unknown type: " + obj); }; // inefficient as hell, but as long as there aren't too many types... global.Functor = function(type, defs) { var oldTypes = types; types = function(f, obj) { if (type.prototype.isPrototypeOf(obj)) { return defs.fmap(f, obj); } return oldTypes(f, obj); } }; global.fmap = curry(function(f, obj) { return types(f, obj); }); }(this));