Skip to content

Instantly share code, notes, and snippets.

@getify
Last active May 31, 2019 11:07
Show Gist options
  • Save getify/0258906a2a6b3b447773cb07e540f565 to your computer and use it in GitHub Desktop.
Save getify/0258906a2a6b3b447773cb07e540f565 to your computer and use it in GitHub Desktop.
Force Monad #MayThe4thBeWithYou
var Force = { Skywalker, RegularFolk, of: Skywalker };
function Skywalker(v) {
return { map, chain, ap };
function map(fn) {
return Skywalker(fn(v));
}
function chain(fn) {
return fn(v);
}
function ap(monad) {
monad.map(v);
}
}
function RegularFolk() {
return { map: RegularFolk, chain: RegularFolk, ap: RegularFolk };
}
var Anakin = Skywalker("Anakin");
var Luke = Anakin.map(x => "Luke");
var Leia = Anakin.map(x => "Leia");
function nonJedi(who) {
if (who == "Rey") return Force.of(who);
return RegularFolk(who);
}
Luke
.chain( () => nonJedi("Rey") )
.chain( () => nonJedi("George Lucas") )
.map( v => w => console.log("Han shot first.") )
.ap( Force.of("Chewbacca") );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment