Last active
September 16, 2016 09:29
-
-
Save MarkusPfundstein/3ee508f2c8de6d6007425ecf3b572df2 to your computer and use it in GitHub Desktop.
eventually correct implementation of IO Monad in JS
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
class IO extends Monad { | |
constructor(fn) { | |
super(); | |
this.__value = fn; | |
} | |
static of(fn) { | |
const io = new IO(() => fn); | |
Object.freeze(io); | |
return io; | |
} | |
map(f) { | |
return new IO(compose(this.__value, f)); | |
} | |
mapP(res, rej) { | |
return this.map(then(res, rej)); | |
} | |
flatMap(f) { | |
/* | |
return new IO(() => { | |
const r = this.map(f).performIO().performIO(); | |
return r; | |
}); | |
*/ | |
// same result as above. So what would be better? | |
return this.map(f).performIO(); | |
} | |
join() { | |
return this.__value; | |
} | |
performIO() { | |
return this.__value(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment