-
Notifications
You must be signed in to change notification settings - Fork 479
Expand file tree
/
Copy pathEither.cs
More file actions
322 lines (286 loc) · 9.52 KB
/
Either.cs
File metadata and controls
322 lines (286 loc) · 9.52 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//Use project level define(s) when referencing with Paket.
//#define CSX_EITHER_INTERNAL // Uncomment this to set visibility to internal.
//#define CSX_REM_MAYBE_FUNC // Uncomment this to remove dependency to Maybe.cs.
using System;
namespace CSharpx
{
#region Either Type
#if !CSX_EITHER_INTERNAL
public
#endif
enum EitherType
{
/// <summary>
/// Failed computation case.
/// </summary>
Left,
/// <summary>
/// Successful computation case.
/// </summary>
Right
}
#if !CSX_EITHER_INTERNAL
public
#endif
abstract class Either<TLeft, TRight>
{
private readonly EitherType tag;
protected Either(EitherType tag)
{
this.tag = tag;
}
public EitherType Tag
{
get { return this.tag; }
}
#region Basic Match Methods
public bool MatchLeft(out TLeft value)
{
value = Tag == EitherType.Left ? ((Left<TLeft, TRight>)this).Value : default(TLeft);
return Tag == EitherType.Left;
}
public bool MatchRight(out TRight value)
{
value = Tag == EitherType.Right ? ((Right<TLeft, TRight>)this).Value : default(TRight);
return Tag == EitherType.Right;
}
#endregion
}
#if !CSX_EITHER_INTERNAL
public
#endif
sealed class Left<TLeft, TRight> : Either<TLeft, TRight>
{
private readonly TLeft value;
internal Left(TLeft value)
: base(EitherType.Left)
{
this.value = value;
}
public TLeft Value
{
get { return value; }
}
}
#if !CSX_EITHER_INTERNAL
public
#endif
sealed class Right<TLeft, TRight> : Either<TLeft, TRight>
{
private readonly TRight value;
internal Right(TRight value)
: base(EitherType.Right)
{
this.value = value;
}
public TRight Value
{
get { return value; }
}
}
#endregion
#if !CSX_EITHER_INTERNAL
public
#endif
static class Either
{
#region Value Case Constructors
public static Either<TLeft, TRight> Left<TLeft, TRight>(TLeft value)
{
return new Left<TLeft, TRight>(value);
}
public static Either<TLeft, TRight> Right<TLeft, TRight>(TRight value)
{
return new Right<TLeft, TRight>(value);
}
#endregion
#region Monad
/// <summary>
/// Inject a value into the Either type, returning Right case.
/// </summary>
public static Either<string, TRight> Return<TRight>(TRight value)
{
return Either.Right<string, TRight>(value);
}
/// <summary>
/// Fail with a message. Not part of mathematical definition of a monad.
/// </summary>
public static Either<string, TRight> Fail<TRight>(string message)
{
throw new Exception(message);
}
/// <summary>
/// Monadic bind.
/// </summary>
public static Either<TLeft, TResult> Bind<TLeft, TRight, TResult>(Either<TLeft, TRight> either, Func<TRight, Either<TLeft, TResult>> func)
{
TRight right;
if (either.MatchRight(out right))
{
return func(right);
}
return Either.Left<TLeft, TResult>(either.GetLeft());
}
#endregion
#region Functor
/// <summary>
/// Transforms a Either's right value by using a specified mapping function.
/// </summary>
public static Either<TLeft, TResult> Map<TLeft, TRight, TResult>(Either<TLeft, TRight> either, Func<TRight, TResult> func)
{
TRight right;
if (either.MatchRight(out right))
{
return Either.Right<TLeft, TResult>(func(right));
}
return Either.Left<TLeft, TResult>(either.GetLeft());
}
#endregion
#region Bifunctor
/// <summary>
/// Maps both parts of a Either type. Applies the first function if Either is Left.
/// Otherwise applies the second function.
/// </summary>
public static Either<TLeft1, TRight1> Bimap<TLeft, TRight, TLeft1, TRight1>(Either<TLeft, TRight> either, Func<TLeft, TLeft1> mapLeft, Func<TRight, TRight1> mapRight)
{
TRight right;
if (either.MatchRight(out right))
{
return Either.Right<TLeft1, TRight1>(mapRight(right));
}
return Either.Left<TLeft1, TRight1>(mapLeft(either.GetLeft()));
}
#endregion
#region Linq Operators
/// <summary>
/// Map operation compatible with Linq.
/// </summary>
public static Either<TLeft, TResult> Select<TLeft, TRight, TResult>(
this Either<TLeft, TRight> either,
Func<TRight, TResult> selector)
{
return Either.Map(either, selector);
}
public static Either<TLeft, TResult> SelectMany<TLeft, TRight, TResult>(this Either<TLeft, TRight> result,
Func<TRight, Either<TLeft, TResult>> func)
{
return Either.Bind(result, func);
}
#endregion
/// <summary>
/// Returns a Either Right or fail with an exception.
/// </summary>
public static TRight GetOrFail<TLeft, TRight>(Either<TLeft, TRight> either)
{
TRight value;
if (either.MatchRight(out value))
return value;
throw new ArgumentException("either", string.Format("The either value was Left {0}", either));
}
/// <summary>
/// Returns a Either Left or a defualt value.
/// </summary>
public static TLeft GetLeftOrDefault<TLeft, TRight>(Either<TLeft, TRight> either, TLeft @default)
{
TLeft value;
return either.MatchLeft(out value) ? value : @default;
}
/// <summary>
/// Returns a Either Right or a defualt value.
/// </summary>
public static TRight GetRightOrDefault<TLeft, TRight>(Either<TLeft, TRight> either, TRight @default)
{
TRight value;
return either.MatchRight(out value) ? value : @default;
}
/// <summary>
/// Wraps a function, encapsulates any exception thrown within to a Either.
/// </summary>
public static Either<Exception, TRight> Try<TRight>(Func<TRight> func)
{
try
{
return new Right<Exception, TRight>(func());
}
catch (Exception ex)
{
return new Left<Exception, TRight>(ex);
}
}
/// <summary>
/// Attempts to cast an object.
/// Stores the cast value in 1Of2 if successful, otherwise stores the exception in 2Of2
/// </summary>
public static Either<Exception, TRight> Cast<TRight>(object obj)
{
return Either.Try(() => (TRight)obj);
}
#if !CSX_REM_MAYBE_FUNC
public static Either<TLeft, TRight> OfMaybe<TLeft, TRight>(Maybe<TRight> maybe, TLeft left)
{
if (maybe.Tag == MaybeType.Just)
{
return Either.Right<TLeft, TRight>(((Just<TRight>)maybe).Value);
}
return Either.Left<TLeft, TRight>(left);
}
#endif
private static TLeft GetLeft<TLeft, TRight>(this Either<TLeft, TRight> either)
{
return ((Left<TLeft, TRight>)either).Value;
}
}
#if !CSX_EITHER_INTERNAL
public
#endif
static class EitherExtensions
{
#region Alternative Match Methods
public static void Match<TLeft, TRight>(this Either<TLeft, TRight> either, Action<TLeft> ifLeft, Action<TRight> ifRight)
{
TLeft left;
if (either.MatchLeft(out left))
{
ifLeft(left);
return;
}
ifRight(((Right<TLeft, TRight>)either).Value);
}
#endregion
/// <summary>
/// Equivalent to monadic <see cref="CSharpx.Either.Return{TRight}"/> operation.
/// Builds a <see cref="CSharpx.Right{TLeft, TRight}"/> value in case <paramref name="value"/> by default.
/// </summary>
public static Either<string, TRight> ToEither<TRight>(this TRight value)
{
return Either.Return<TRight>(value);
}
public static Either<TLeft, TResult> Bind<TLeft, TRight, TResult>(
this Either<TLeft, TRight> either,
Func<TRight, Either<TLeft, TResult>> func)
{
return Either.Bind(either, func);
}
public static Either<TLeft, TResult> Map<TLeft, TRight, TResult>(
this Either<TLeft, TRight> either,
Func<TRight, TResult> func)
{
return Either.Map(either, func);
}
public static Either<TLeft1, TRight1> Bimap<TLeft, TRight, TLeft1, TRight1>(
this Either<TLeft, TRight> either,
Func<TLeft, TLeft1> mapLeft,
Func<TRight, TRight1> mapRight)
{
return Either.Bimap(either, mapLeft, mapRight);
}
public static bool IsLeft<TLeft, TRight>(this Either<TLeft, TRight> either)
{
return either.Tag == EitherType.Left;
}
public static bool IsRight<TLeft, TRight>(this Either<TLeft, TRight> either)
{
return either.Tag == EitherType.Right;
}
}
}