-
-
Save getify/0258906a2a6b3b447773cb07e540f565 to your computer and use it in GitHub Desktop.
Force Monad #MayThe4thBeWithYou
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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