-
Notifications
You must be signed in to change notification settings - Fork 0
/
monad.js
56 lines (51 loc) · 1.36 KB
/
monad.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// IO a :: World -> (a, World)
//
// print :: Show a => a -> IO ()
// print :: Show a => a -> (World -> ((), World))
//
// getChar :: IO Char
//
// (>>=) :: Monad m => m a -> (a -> m b) -> m b
//
"use strict";
function print(a) {
return function (world) {
console.log("print: " + a);
return [undefined, world];
};
}
//print = (a) => (world) => [undefined, [...world, a]]; // bug
var sget = require("sget");
function getStr() {
return function(world) {
var s = sget();
return [s, world];
}
}
/*
function bind(ioa) {
return function(f) {
return function(w0) {
var [a, w1] = ioa(w0);
return f(a)(w1);
};
};
}
*/
//var bind = ioa => f => w0 => { var [a, w1] = ioa(w0); return f(a)(w1); };
var bind = ioa => f => w0 => ( a_w1 => f(a_w1[0])(a_w1[1]) )(ioa(w0));
//var io1 = print("asdf");
//var io2 = bind(io1)((a) => getStr());
//var io3 = bind(io2)((a) => print(a));
var foo = bind(print("First number?"))(function (x1) {
return bind(getStr())(function (x2) {
return bind(print("Got " + parseInt(x2) + ". Second number? "))(function (x3) {
return bind(getStr())(function (x4) {
return print("Got " + parseInt(x4) + ". Sum: " + (parseInt(x2) + parseInt(x4)));
});
});
});
});
console.log("====");
foo("world");
console.log("done");