class Reader {
constructor(f) {
this.f = f
}
run(e) {
return this.f(e)
}
fmap(g) {
return new Reader(e => g(this.run(e)))
}
bind(g) {
return new Reader(e => g(this.run(e)).run(e))
}
}
const returnR = a => new Reader(_ => a)
const ask = new Reader(x => x)
const comp3 = v =>
ask.bind(({myNum}) =>
returnR(myNum * v))
const comp2 = _ =>
ask.bind(({myNum}) =>
returnR(myNum * 3)
)
console.log(
comp2().bind(comp3).fmap(x => x * 100).run({myNum: 5})
)