varfunctor=require('functor');varfmap=functor.fmap;// We can define fmap methods on predefined types using the functor function// N.B. this does not affect any prototype chains or modify external objectsfunctor(Promise,function(fn,functor){// functor here is the promise instancereturnfunctor.then(fn);});functor(Array,function(fn,functor){returnfunctor.map(fn);});// fmap will also look for an fmap method on the objectfunctionNothing(){}Nothing.prototype.fmap=function(){returnNothing()};functionJust(val){this.val=val;}Just.prototype.fmap=function(fn){returnMaybe(fn(this.val))}functionMaybe(val){returnval==null ? Nothing() : Just(val)}functionlogName(obj){console.log(obj.name);returnobj;}varsomePromise=getUserFromServer();// this will log out the name at some point;fmap(logName,somePromise);// returns a new functor, of same type (Promise);varme={name: 'fabien'}// this will log out "fabien"fmap(logName,Maybe(me));// returns new functor (Just)varyou;// this doesn't log out anything, nor does it throw a cannot read property name of nullfmap(logName,Maybe(you));// returns new functor (Nothing)