-
Notifications
You must be signed in to change notification settings - Fork 1
/
IO.js
76 lines (63 loc) · 1.26 KB
/
IO.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict';
var runIO = function (io) {
return io.val.apply(this, [].slice.call(arguments, 1));
};
var IOType = function (fn) {
this.val = fn;
this.runIO = this.val;
};
var IO = function (fn) {
return (new IOType(fn));
};
IOType.of = function (x) {
return IO(function () {
return x;
});
};
IOType.prototype.of = IOType.of;
IOType.prototype.chain = function (g) {
var io = this;
return IO(function () {
return g(io.val()).val();
});
};
// Derived
IOType.prototype.map = function (f) {
return this.chain(function (a) {
return IOType.of(f(a));
});
};
IOType.prototype.ap = function (a) {
return this.chain(function (f) {
return a.map(f);
});
};
var extendFn = function () {
Function.prototype.toIO = function () {
var self = this;
return function (x) {
var args = arguments;
return IO(function () {
return self.apply(this, args)
});
};
};
};
var inspect = function (x) {
if (x == null || x == undefined) return "null";
return x.inspect ? x.inspect() : x.toString();
};
IOType.prototype.inspect = function () {
return 'IO(' + inspect(this.val) + ')';
};
IOType.prototype.toString = function () {
return this.inspect();
};
IO.of = function (x) {
return IO(x).of(x);
};
module.exports = {
IO: IO,
runIO: runIO,
extendFn: extendFn
};