/
either.dart
216 lines (170 loc) · 6.12 KB
/
either.dart
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import 'dart:async';
typedef Lazy<T> = T Function();
/// Represents a value of one of two possible types.
/// Instances of [Either] are either an instance of [Left] or [Right].
///
/// [Left] is used for "failure".
/// [Right] is used for "success".
abstract class Either<L, R> {
const Either();
/// Represents the left side of [Either] class which by convention is a "Failure".
bool get isLeft => this is Left<L, R>;
/// Represents the right side of [Either] class which by convention is a "Success"
bool get isRight => this is Right<L, R>;
/// Get [Left] value, may throw an exception when the value is [Right]
L get left => this.fold<L>(
(value) => value,
(right) => throw Exception(
'Illegal use. You should check isLeft before calling'));
/// Get [Right] value, may throw an exception when the value is [Left]
R get right => this.fold<R>(
(left) => throw Exception(
'Illegal use. You should check isRight before calling'),
(value) => value);
/// Transform values of [Left] and [Right]
Either<TL, TR> either<TL, TR>(
TL Function(L left) fnL, TR Function(R right) fnR);
/// Transform value of [Right] when transformation may be finished with an error
Either<L, TR> then<TR>(Either<L, TR> Function(R right) fnR);
/// Transform value of [Right] when transformation may be finished with an error
Future<Either<L, TR>> thenAsync<TR>(
FutureOr<Either<L, TR>> Function(R right) fnR);
/// Transform value of [Left] when transformation may be finished with an [Right]
Either<TL, R> thenLeft<TL>(Either<TL, R> Function(L left) fnL);
/// Transform value of [Left] when transformation may be finished with an [Right]
Future<Either<TL, R>> thenLeftAsync<TL>(
FutureOr<Either<TL, R>> Function(L left) fnL);
/// Transform value of [Right]
Either<L, TR> map<TR>(TR Function(R right) fnR);
/// Transform value of [Left]
Either<TL, R> mapLeft<TL>(TL Function(L left) fnL);
/// Transform value of [Right]
Future<Either<L, TR>> mapAsync<TR>(FutureOr<TR> Function(R right) fnR);
/// Transform value of [Left]
Future<Either<TL, R>> mapLeftAsync<TL>(FutureOr<TL> Function(L left) fnL);
/// Fold [Left] and [Right] into the value of one type
T fold<T>(T Function(L left) fnL, T Function(R right) fnR);
/// Swap [Left] and [Right]
Either<R, L> swap() => fold((left) => Right(left), (right) => Left(right));
/// Constructs a new [Either] from a function that might throw
static Either<L, R> tryCatch<L, R, Err extends Object>(
L Function(Err err) onError, R Function() fnR) {
try {
return Right(fnR());
} on Err catch (e) {
return Left(onError(e));
}
}
/// If the condition is satify then return [rightValue] in [Right] else [leftValue] in [Left]
static Either<L, R> cond<L, R>(bool test, L leftValue, R rightValue) =>
test ? Right(rightValue) : Left(leftValue);
/// If the condition is satify then return [rightValue] in [Right] else [leftValue] in [Left]
static Either<L, R> condLazy<L, R>(
bool test, Lazy<L> leftValue, Lazy<R> rightValue) =>
test ? Right(rightValue()) : Left(leftValue());
@override
bool operator ==(Object obj) {
return this.fold(
(left) => obj is Left && left == obj.value,
(right) => obj is Right && right == obj.value,
);
}
@override
int get hashCode => fold((left) => left.hashCode, (right) => right.hashCode);
}
/// Used for "failure"
class Left<L, R> extends Either<L, R> {
final L value;
const Left(this.value);
@override
Either<TL, TR> either<TL, TR>(
TL Function(L left) fnL, TR Function(R right) fnR) {
return Left<TL, TR>(fnL(value));
}
@override
Either<L, TR> then<TR>(Either<L, TR> Function(R right) fnR) {
return Left<L, TR>(value);
}
@override
Future<Either<L, TR>> thenAsync<TR>(
FutureOr<Either<L, TR>> Function(R right) fnR) {
return Future.value(Left<L, TR>(value));
}
@override
Either<TL, R> thenLeft<TL>(Either<TL, R> Function(L left) fnL) {
return fnL(value);
}
@override
Future<Either<TL, R>> thenLeftAsync<TL>(
FutureOr<Either<TL, R>> Function(L left) fnL) {
return Future.value(fnL(value));
}
@override
Either<L, TR> map<TR>(TR Function(R right) fnR) {
return Left<L, TR>(value);
}
@override
Either<TL, R> mapLeft<TL>(TL Function(L left) fnL) {
return Left<TL, R>(fnL(value));
}
@override
Future<Either<L, TR>> mapAsync<TR>(FutureOr<TR> Function(R right) fnR) {
return Future.value(Left<L, TR>(value));
}
@override
Future<Either<TL, R>> mapLeftAsync<TL>(FutureOr<TL> Function(L left) fnL) {
return Future.value(fnL(value)).then((value) => Left<TL, R>(value));
}
@override
T fold<T>(T Function(L left) fnL, T Function(R right) fnR) {
return fnL(value);
}
}
/// Used for "success"
class Right<L, R> extends Either<L, R> {
final R value;
const Right(this.value);
@override
Either<TL, TR> either<TL, TR>(
TL Function(L left) fnL, TR Function(R right) fnR) {
return Right<TL, TR>(fnR(value));
}
@override
Either<L, TR> then<TR>(Either<L, TR> Function(R right) fnR) {
return fnR(value);
}
@override
Future<Either<L, TR>> thenAsync<TR>(
FutureOr<Either<L, TR>> Function(R right) fnR) {
return Future.value(fnR(value));
}
@override
Either<TL, R> thenLeft<TL>(Either<TL, R> Function(L left) fnL) {
return Right<TL, R>(value);
}
@override
Future<Either<TL, R>> thenLeftAsync<TL>(
FutureOr<Either<TL, R>> Function(L left) fnL) {
return Future.value(Right<TL, R>(value));
}
@override
Either<L, TR> map<TR>(TR Function(R right) fnR) {
return Right<L, TR>(fnR(value));
}
@override
Either<TL, R> mapLeft<TL>(TL Function(L left) fnL) {
return Right<TL, R>(value);
}
@override
Future<Either<L, TR>> mapAsync<TR>(FutureOr<TR> Function(R right) fnR) {
return Future.value(fnR(value)).then((value) => Right<L, TR>(value));
}
@override
Future<Either<TL, R>> mapLeftAsync<TL>(FutureOr<TL> Function(L left) fnL) {
return Future.value(Right<TL, R>(value));
}
@override
T fold<T>(T Function(L left) fnL, T Function(R right) fnR) {
return fnR(value);
}
}