Last active
February 17, 2017 13:10
-
-
Save kwijibo/8463ffb0813d2dd866405d9e432755c5 to your computer and use it in GitHub Desktop.
staticland monads
This file contains hidden or 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
const Reader = { | |
of: a => { return _ => a }, | |
map: f => run => { | |
return x => f(run(x)) | |
}, | |
contramap: f => run => { | |
return x => run(f(x)) | |
}, | |
chain: f => run => { | |
return x => f(run(x))(x) | |
}, | |
run: x => run => run(x) | |
} | |
pipe( | |
Reader.map(x => x * 2), | |
Reader.chain(x => Reader.of(x * 10)), | |
Reader.run(3) | |
)(x => x) //=> 60 |
This file contains hidden or 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
const stringise = f => str => {f.toString = () => str; return f} | |
const Task = { | |
of: x => stringise((rej, res)=>{ res(x)})(`Task(${x})`), | |
empty: () =>{}, | |
map: f => fork => (rej, res) => fork(rej, x=>res(f(x))), | |
chain: f => fork => (rej, res) => fork(rej, data => f(data)(rej, res)), | |
run: (err, success) => fork => fork(err, success) | |
} | |
pipe( | |
Task.of | |
, Task.map(i=>i+1) | |
, Task.chain(i => Task.of(i*8)) | |
, Task.run(console.error, console.log) | |
)(999) | |
Task.of(3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment