Created
September 10, 2018 07:03
-
-
Save gdeest/bb4463bfcfc0741548302b013cefe504 to your computer and use it in GitHub Desktop.
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
| // IO a ~ () -> a | |
| class IO { | |
| constructor(op) { | |
| // IO a -> a | |
| this.run = op; | |
| } | |
| // a -> IO a | |
| static returnIO(a) { | |
| return new IO(() => a); | |
| } | |
| // IO a -> (a -> IO b) -> IO b | |
| bind(k) { | |
| return new IO(() => k(this.run()).run()); | |
| } | |
| } | |
| const putStrLn = text => new IO(() => console.log(text)); | |
| const getDateTime = new IO(Date.now); | |
| IO.returnIO("Hello, world !") | |
| .bind(putStrLn) | |
| .bind(_ => getDateTime) | |
| .bind(putStrLn) | |
| .run(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment