-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathEither.js
135 lines (101 loc) · 2.76 KB
/
Either.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var curry = require('ramda/src/curry');
var toString = require('ramda/src/toString');
var util = require('./internal/util');
function Either(left, right) {
switch (arguments.length) {
case 0:
throw new TypeError('no arguments to Either');
case 1:
return function(right) {
return right == null ? Either.Left(left) : Either.Right(right);
};
default:
return right == null ? Either.Left(left) : Either.Right(right);
}
}
Either.prototype['@@type'] = 'ramda-fantasy/Either';
Either.prototype.map = util.returnThis;
Either.of = Either.prototype.of = function(value) {
return Either.Right(value);
};
Either.prototype.chain = util.returnThis; // throw?
Either.either = curry(function either(leftFn, rightFn, e) {
if (e instanceof _Left) {
return leftFn(e.value);
} else if (e instanceof _Right) {
return rightFn(e.value);
} else {
throw new TypeError('invalid type given to Either.either');
}
});
Either.isLeft = function(x) {
return x.isLeft;
};
Either.isRight = function(x) {
return x.isRight;
};
// Right
function _Right(x) {
this.value = x;
}
util.extend(_Right, Either);
_Right.prototype.isRight = true;
_Right.prototype.isLeft = false;
_Right.prototype.map = function(fn) {
return new _Right(fn(this.value));
};
_Right.prototype.ap = function(that) {
return that.map(this.value);
};
_Right.prototype.chain = function(f) {
return f(this.value);
};
//chainRec
Either.chainRec = Either.prototype.chainRec = function(f, i) {
var res, state = util.chainRecNext(i);
while (state.isNext) {
res = f(util.chainRecNext, util.chainRecDone, state.value);
if (Either.isLeft(res)) {
return res;
}
state = res.value;
}
return Either.Right(state.value);
};
_Right.prototype.bimap = function(_, f) {
return new _Right(f(this.value));
};
_Right.prototype.extend = function(f) {
return new _Right(f(this));
};
_Right.prototype.toString = function() {
return 'Either.Right(' + toString(this.value) + ')';
};
_Right.prototype.equals = util.getEquals(_Right);
Either.Right = function(value) {
return new _Right(value);
};
// Left
function _Left(x) {
this.value = x;
}
util.extend(_Left, Either);
_Left.prototype.isLeft = true;
_Left.prototype.isRight = false;
_Left.prototype.ap = util.returnThis;
_Left.prototype.bimap = function(f) {
return new _Left(f(this.value));
};
_Left.prototype.extend = util.returnThis;
_Left.prototype.toString = function() {
return 'Either.Left(' + toString(this.value) + ')';
};
_Left.prototype.equals = util.getEquals(_Left);
Either.Left = function(value) {
return new _Left(value);
};
// either
Either.prototype.either = function instanceEither(leftFn, rightFn) {
return this.isLeft ? leftFn(this.value) : rightFn(this.value);
};
module.exports = Either;