Skip to content

Instantly share code, notes, and snippets.

@gdeest
Created September 10, 2018 07:03
Show Gist options
  • Select an option

  • Save gdeest/bb4463bfcfc0741548302b013cefe504 to your computer and use it in GitHub Desktop.

Select an option

Save gdeest/bb4463bfcfc0741548302b013cefe504 to your computer and use it in GitHub Desktop.
// 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