Skip to content

Instantly share code, notes, and snippets.

@ympbyc
Forked from hiratara/monad.js
Created December 23, 2012 04:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ympbyc/4362024 to your computer and use it in GitHub Desktop.
Save ympbyc/4362024 to your computer and use it in GitHub Desktop.
// IO a -> a
function unsafePerformIO (io) {
var tuple = io({"memo" : "This is the real world!!"});
var newWorld = tuple[0];
var value = tuple[1];
return value;
}
// IO a -> (a -> IO b) -> IO b
function bind (io1, f) {
return function (world) {
var tuple = io1(world);
var newWorld = tuple[0];
var value = tuple[1];
var io2 = f(value);
return io2(newWorld);
};
}
// String -> IO ()
function alertIO (message) {
return function (world) {
alert(message);
var newWorld = world;
newWorld["alerted"] = "DONE";
return [newWorld, null];
};
}
// IO Date
function dateIO (world) {
return [world, new Date()];
}
// (Date -> IO ()) -> IO ()
var doItWithDate = function(f) {
return bind(dateIO, f);
};
// IO ()
var main = doItWithDate(alertIO);
unsafePerformIO(main);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment