-
Notifications
You must be signed in to change notification settings - Fork 479
Expand file tree
/
Copy pathMaybe.cs
More file actions
397 lines (362 loc) · 12.1 KB
/
Maybe.cs
File metadata and controls
397 lines (362 loc) · 12.1 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
//Use project level define(s) when referencing with Paket.
//#define CSX_MAYBE_INTERNAL // Uncomment this to set visibility to internal.
//#define CSX_REM_EITHER_FUNC // Uncomment this to remove dependency to Either.cs.
using System;
using System.Collections.Generic;
using System.Linq;
namespace CSharpx
{
#region Maybe Type
/// <summary>
/// Discriminator for <see cref="CSharpx.Maybe"/>.
/// </summary>
#if !CSX_MAYBE_INTERNAL
public
#endif
enum MaybeType
{
Just,
Nothing
}
/// <summary>
/// The Maybe type models an optional value. A value of type Maybe a either contains a value of type a (represented as Just a),
/// or it is empty (represented as Nothing).
/// </summary>
#if !CSX_MAYBE_INTERNAL
public
#endif
abstract class Maybe<T>
{
private readonly MaybeType tag;
protected Maybe(MaybeType tag)
{
this.tag = tag;
}
/// <summary>
/// Type discriminator.
/// </summary>
public MaybeType Tag { get { return tag; } }
#region Basic Match Methods
/// <summary>
/// Matches a value returning <c>true</c> and value itself via output parameter.
/// </summary>
public bool MatchJust(out T value)
{
value = Tag == MaybeType.Just ? ((Just<T>)this).Value : default(T);
return Tag == MaybeType.Just;
}
/// <summary>
/// Matches an empty value returning <c>true</c>.
/// </summary>
public bool MatchNothing()
{
return Tag == MaybeType.Nothing;
}
#endregion
}
#endregion
/// <summary>
/// Models a <see cref="CSharpx.Maybe"/> when in empty state.
/// </summary>
#if !CSX_MAYBE_INTERNAL
public
#endif
sealed class Nothing<T> : Maybe<T>
{
internal Nothing()
: base(MaybeType.Nothing)
{
}
}
/// <summary>
/// Models a <see cref="CSharpx.Maybe"/> when contains a value.
/// </summary>
#if !CSX_MAYBE_INTERNAL
public
#endif
sealed class Just<T> : Maybe<T>
{
private readonly T value;
internal Just(T value)
: base(MaybeType.Just)
{
this.value = value;
}
/// <summary>
/// The wrapped value.
/// </summary>
public T Value
{
get { return value; }
}
}
/// <summary>
/// Provides static methods for manipulating <see cref="CSharpx.Maybe"/>.
/// </summary>
#if !CSX_MAYBE_INTERNAL
public
#endif
static class Maybe
{
#region Value Case Constructors
/// <summary>
/// Builds the empty case of <see cref="CSharpx.Maybe"/>.
/// </summary>
public static Maybe<T> Nothing<T>()
{
return new Nothing<T>();
}
/// <summary>
/// Builds the case when <see cref="CSharpx.Maybe"/> contains a value.
/// </summary>
public static Just<T> Just<T>(T value)
{
return new Just<T>(value);
}
#endregion
#region Monad
/// <summary>
/// Inject a value into the monadic <see cref="CSharpx.Maybe{T}"/> type.
/// </summary>
public static Maybe<T> Return<T>(T value)
{
return Equals(value, default(T)) ? Maybe.Nothing<T>() : Maybe.Just(value);
}
/// <summary>
/// Sequentially compose two actions, passing any value produced by the first as an argument to the second.
/// </summary>
public static Maybe<T2> Bind<T1, T2>(Maybe<T1> maybe, Func<T1, Maybe<T2>> func)
{
T1 value1;
return maybe.MatchJust(out value1) ? func(value1) : Maybe.Nothing<T2>();
}
#endregion
#region Functor
/// <summary>
/// Transforms an maybe value by using a specified mapping function.
/// </summary>
public static Maybe<T2> Map<T1, T2>(Maybe<T1> maybe, Func<T1, T2> func)
{
T1 value1;
return maybe.MatchJust(out value1) ? Maybe.Just(func(value1)) : Maybe.Nothing<T2>();
}
#endregion
/// <summary>
/// If both maybes contain a value, it merges them into a maybe with a tupled value.
/// </summary>
public static Maybe<Tuple<T1, T2>> Merge<T1, T2>(Maybe<T1> first, Maybe<T2> second)
{
T1 value1;
T2 value2;
if (first.MatchJust(out value1) && second.MatchJust(out value2))
{
return Maybe.Just(Tuple.Create(value1, value2));
}
return Maybe.Nothing<Tuple<T1, T2>>();
}
#if !CSX_REM_EITHER_FUNC
/// <summary>
/// Maps Either Right value to Maybe Just, otherwise Maybe Nothing.
/// </summary>
public static Maybe<TRight> OfEither<TLeft, TRight>(Either<TLeft, TRight> either)
{
if (either.Tag == EitherType.Right)
{
return Maybe.Just(((Right<TLeft, TRight>)either).Value);
}
return Maybe.Nothing<TRight>();
}
#endif
}
/// <summary>
/// Provides convenience extension methods for <see cref="CSharpx.Maybe"/>.
/// </summary>
#if !CSX_MAYBE_INTERNAL
public
#endif
static class MaybeExtensions
{
#region Alternative Match Methods
/// <summary>
/// Provides pattern matching using <see cref="System.Action"/> delegates.
/// </summary>
public static void Match<T>(this Maybe<T> maybe, Action<T> ifJust, Action ifNothing)
{
T value;
if (maybe.MatchJust(out value))
{
ifJust(value);
return;
}
ifNothing();
}
/// <summary>
/// Provides pattern matching using <see cref="System.Action"/> delegates over maybe with tupled wrapped value.
/// </summary>
public static void Match<T1, T2>(this Maybe<Tuple<T1, T2>> maybe, Action<T1, T2> ifJust, Action ifNothing)
{
T1 value1;
T2 value2;
if (maybe.MatchJust(out value1, out value2))
{
ifJust(value1, value2);
return;
}
ifNothing();
}
/// <summary>
/// Matches a value returning <c>true</c> and tupled value itself via two output parameters.
/// </summary>
public static bool MatchJust<T1, T2>(this Maybe<Tuple<T1, T2>> maybe, out T1 value1, out T2 value2)
{
Tuple<T1, T2> value;
if (maybe.MatchJust(out value))
{
value1 = value.Item1;
value2 = value.Item2;
return true;
}
value1 = default(T1);
value2 = default(T2);
return false;
}
#endregion
/// <summary>
/// Equivalent to monadic <see cref="CSharpx.Maybe.Return{T}"/> operation.
/// Builds a <see cref="CSharpx.Just{T}"/> value in case <paramref name="value"/> is different from its default.
/// </summary>
public static Maybe<T> ToMaybe<T>(this T value)
{
return Maybe.Return(value);
}
/// <summary>
/// Invokes a function on this maybe value that itself yields a maybe.
/// </summary>
public static Maybe<T2> Bind<T1, T2>(this Maybe<T1> maybe, Func<T1, Maybe<T2>> func)
{
return Maybe.Bind(maybe, func);
}
/// <summary>
/// Transforms this maybe value by using a specified mapping function.
/// </summary>
public static Maybe<T2> Map<T1, T2>(this Maybe<T1> maybe, Func<T1, T2> func)
{
return Maybe.Map(maybe, func);
}
#region Linq Operators
/// <summary>
/// Map operation compatible with Linq.
/// </summary>
public static Maybe<TResult> Select<TSource, TResult>(
this Maybe<TSource> maybe,
Func<TSource, TResult> selector)
{
return Maybe.Map(maybe, selector);
}
/// <summary>
/// Bind operation compatible with Linq.
/// </summary>
public static Maybe<TResult> SelectMany<TSource, TValue, TResult>(
this Maybe<TSource> maybe,
Func<TSource, Maybe<TValue>> valueSelector,
Func<TSource, TValue, TResult> resultSelector)
{
return maybe
.Bind(sourceValue =>
valueSelector(sourceValue)
.Map(resultValue => resultSelector(sourceValue, resultValue)));
}
#endregion
#region Do Semantic
/// <summary>
/// If contains a value executes an <see cref="System.Action{T}"/> delegate over it.
/// </summary>
public static void Do<T>(this Maybe<T> maybe, Action<T> action)
{
T value;
if (maybe.MatchJust(out value))
{
action(value);
}
}
/// <summary>
/// If contains a value executes an <see cref="System.Action{T1, T2}"/> delegate over it.
/// </summary>
public static void Do<T1, T2>(this Maybe<Tuple<T1, T2>> maybe, Action<T1, T2> action)
{
T1 value1;
T2 value2;
if (maybe.MatchJust(out value1, out value2))
{
action(value1, value2);
}
}
#endregion
/// <summary>
/// Returns <c>true</c> iffits argument is of the form <see cref="CSharpx.Just{T}"/>.
/// </summary>
public static bool IsJust<T>(this Maybe<T> maybe)
{
return maybe.Tag == MaybeType.Just;
}
/// <summary>
/// Returns <c>true</c> iffits argument is of the form <see cref="CSharpx.Nothing{T}"/>.
/// </summary>
public static bool IsNothing<T>(this Maybe<T> maybe)
{
return maybe.Tag == MaybeType.Nothing;
}
/// <summary>
/// Extracts the element out of a <see cref="CSharpx.Just{T}"/> and returns a default value if its argument is <see cref="CSharpx.Nothing{T}"/>.
/// </summary>
public static T FromJust<T>(this Maybe<T> maybe)
{
T value;
if (maybe.MatchJust(out value))
{
return value;
}
return default(T);
}
/// <summary>
/// Extracts the element out of a <see cref="CSharpx.Just{T}"/> and throws an error if its argument is <see cref="CSharpx.Nothing{T}"/>.
/// </summary>
public static T FromJustOrFail<T>(this Maybe<T> maybe, Exception exceptionToThrow = null)
{
T value;
if (maybe.MatchJust(out value))
{
return value;
}
throw exceptionToThrow ?? new ArgumentException("Value empty.");
}
/// <summary>
/// If contains a values returns it, otherwise returns <paramref name="noneValue"/>.
/// </summary>
public static T GetValueOrDefault<T>(this Maybe<T> maybe, T noneValue)
{
T value;
return maybe.MatchJust(out value) ? value : noneValue;
}
/// <summary>
/// If contains a values executes a mapping function over it, otherwise returns <paramref name="noneValue"/>.
/// </summary>
public static T2 MapValueOrDefault<T1, T2>(this Maybe<T1> maybe, Func<T1, T2> func, T2 noneValue)
{
T1 value1;
return maybe.MatchJust(out value1) ? func(value1) : noneValue;
}
/// <summary>
/// Returns an empty list when given <see cref="CSharpx.Nothing{T}"/> or a singleton list when given a <see cref="CSharpx.Just{T}"/>.
/// </summary>
public static IEnumerable<T> ToEnumerable<T>(this Maybe<T> maybe)
{
T value;
if (maybe.MatchJust(out value))
{
return Enumerable.Empty<T>().Concat(new[] { value });
}
return Enumerable.Empty<T>();
}
}
}