Skip to content

Instantly share code, notes, and snippets.

@kwijibo
Last active February 17, 2017 13:10
Show Gist options
  • Save kwijibo/8463ffb0813d2dd866405d9e432755c5 to your computer and use it in GitHub Desktop.
Save kwijibo/8463ffb0813d2dd866405d9e432755c5 to your computer and use it in GitHub Desktop.
staticland monads
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
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