-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheither.js
More file actions
41 lines (33 loc) · 716 Bytes
/
either.js
File metadata and controls
41 lines (33 loc) · 716 Bytes
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
const Left = x => ({
ap: fx => fx.map(x),
chain: f => Left(x),
map: f => Left(x),
fold: (f, g) => f(x),
inspect: () => `Left( ${x} )`
});
Left.of = x => Left(x);
const Right = x => ({
ap: fx => fx.map(x),
chain: f => f(x),
map: f => Right(f(x)),
fold: (f, g) => g(x),
inspect: () => `Right( ${x} )`
});
Right.of = x => Right(x);
const fromNullable = x =>
x ? Right(x) : Left(null);
const Either = x =>
x ? Right(x) : Left(null);
Either.of = Right.of;
const tryCatch = f => {
try {
return Right(f());
} catch (e) {
return Left(e);
}
};
exports.Left = Left;
exports.Right = Right;
exports.fromNullable = fromNullable;
exports.tryCatch = tryCatch;
exports.Either = Either;