1 | "use strict";
|
2 |
|
3 | var assert = require("assert");
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | function Left(value) {
|
10 | this.value = value;
|
11 | }
|
12 |
|
13 | function Right(value) {
|
14 | this.value = value;
|
15 | }
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | function left(value) {
|
21 | return new Left(value);
|
22 | }
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | function right(value) {
|
28 | return new Right(value);
|
29 | }
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | Left.prototype.either = function lefteither(l) {
|
35 | return l(this.value);
|
36 | };
|
37 |
|
38 | Right.prototype.either = function righteither(l, r) {
|
39 | return r(this.value);
|
40 | };
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | Left.prototype.isEqual = function leftIsEqual(other) {
|
48 | assert(other instanceof Left || other instanceof Right, "isEqual: `other` parameter should be either");
|
49 | return other instanceof Left && this.value === other.value;
|
50 | };
|
51 |
|
52 | Right.prototype.isEqual = function rightIsEqual(other) {
|
53 | assert(other instanceof Left || other instanceof Right, "isEqual: `other` parameter should be either");
|
54 | return other instanceof Right && this.value === other.value;
|
55 | };
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | Left.prototype.bimap = function leftBimap(f) {
|
66 | return new Left(f(this.value));
|
67 | };
|
68 |
|
69 | Right.prototype.bimap = function rightBimap(f, g) {
|
70 | return new Right(g(this.value));
|
71 | };
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | Left.prototype.first = function leftFirst(f) {
|
81 | return new Left(f(this.value));
|
82 | };
|
83 |
|
84 | Right.prototype.first = function rightFirst() {
|
85 | return this;
|
86 | };
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | Left.prototype.second = function leftSecond() {
|
96 | return this;
|
97 | };
|
98 |
|
99 | Right.prototype.second = function rightSecond(g) {
|
100 | return new Right(g(this.value));
|
101 | };
|
102 |
|
103 | module.exports = {
|
104 | left: left,
|
105 | right: right,
|
106 | };
|