Skip to content

Instantly share code, notes, and snippets.

@getify getify/1.js
Last active May 31, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.