Permalink
Browse files

added monad.js (from monadic-node repository)

  • Loading branch information...
1 parent 3892435 commit f682e1db852d31dfa069130c62806dd1cc304719 @KOBA789 committed Dec 15, 2011
Showing with 80 additions and 0 deletions.
  1. +80 −0 monad.js
View
@@ -0,0 +1,80 @@
+var fs = require('fs');
+
+var noop = function () {};
+
+var monadize = function (f) {
+ f.bind = function (f2) {
+ return bind(f, f2);
+ };
+ f.push = function (f2) {
+ return push(f, f2);
+ };
+ f['>>='] = f.bind;
+ f['>>'] = push;
+ return f;
+};
+
+var _return = function (val) {
+ return monadize(function (next) {
+ process.nextTick(function () {
+ next(val);
+ });
+ });
+};
+
+var bind = function (monad, func) {
+ return monadize(function (next) {
+ if (typeof next !== 'function') { next = noop; }
+ monad(function () {
+ func.apply(null, arguments)(next);
+ });
+ });
+};
+
+var push = function (monad, func) {
+ return monadize(function (next) {
+ if (typeof next !== 'function') { next = noop; }
+ monad(function () {
+ func(next);
+ });
+ });
+};
+
+var putStrLn = function (str) {
+ return monadize(function (next) {
+ console.log(str);
+ process.nextTick(next);
+ });
+};
+
+var wait = function (time) {
+ return monadize(function (next) {
+ setTimeout(next, time);
+ });
+};
+
+var readFile = function (file) {
+ return monadize(function (next) {
+ fs.readFile(file, 'utf-8', function (err, data) {
+ if (err) throw err;
+ next(data);
+ });
+ });
+};
+
+var _do = monadize(function (next) {
+ process.nextTick(next);
+});
+
+var main;
+
+main = _do
+ .push( _return('hello') )
+ .bind( putStrLn )
+ .push( wait(1000) )
+ .push( putStrLn('world') )
+ .push( readFile('./a.txt') )
+ .bind( putStrLn )
+;
+
+main();

0 comments on commit f682e1d

Please sign in to comment.