-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathMaybe.cs
More file actions
executable file
·244 lines (197 loc) · 7.74 KB
/
Maybe.cs
File metadata and controls
executable file
·244 lines (197 loc) · 7.74 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Data.Maybe
{
public struct Maybe<T>
{
public readonly static Maybe<T> Nothing = new Maybe<T>();
public T Value;
public bool HasValue;
public Maybe(T value) {
Value = value;
HasValue = true;
}
public override string ToString() {
if (!HasValue) {
return "<Nothing>";
}
return Value.ToString();
}
}
public static class Maybe
{
public static string FromMaybeS<T>(this Maybe<T> a, string @default) {
return a.HasValue ? a.Value.ToString() : @default;
}
public static T FromMaybe<T>(this Maybe<T> a, Func<Exception> e) {
if (a.IsNothing()) {
throw e();
}
return a.Value;
}
public static T FromMaybe<T>(this Maybe<T> a, Func<T> @default) {
return a.HasValue ? a.Value : @default();
}
public static IEnumerable<T> FromMaybeToList<T>(this Maybe<T> a) {
if (a.IsSomething())
yield return a.Value;
}
public static T FromMaybeOrNull<T>(this Maybe<T> a) where T : class {
return a.IsNothing() ? null : a.Value;
}
public static T FromMaybeOrDefault<T>(this Maybe<T> a) {
return a.FromMaybe(default(T));
}
public static T FromMaybe<T>(this Maybe<T> a, T @default) {
return a.HasValue ? a.Value : @default;
}
public static T2 FromMaybe<T, T2>(this Maybe<T> a, Func<T, T2> fn, Func<T2> @default) {
return a.HasValue ? fn(a.Value) : @default();
}
public static T2 FromMaybe<T, T2>(this Maybe<T> a, Func<T, T2> fn, T2 @default) {
return a.HasValue ? fn(a.Value) : @default;
}
public static Maybe<B> As<T, B>(this Maybe<T> a) where B : class {
return from m in a
let t = m as B
where t != null
select t;
}
public static Maybe<T> Cast<T>(this object a) {
try {
var t = (T)a;
return t.ToMaybe();
}
catch {
return Maybe<T>.Nothing;
}
}
public static T? ToNullable<T>(this Maybe<T> a) where T : struct {
if (a.IsSomething())
return new Nullable<T>(a.Value);
else
return new Nullable<T>();
}
public static Maybe<T> ToMaybe<T>(this Nullable<T> a) where T : struct {
if (!a.HasValue)
return Maybe<T>.Nothing;
return a.Value.ToMaybe();
}
public static Maybe<T> ToMaybe<T>(this T a) {
if (a == null)
return Maybe<T>.Nothing;
return new Maybe<T>(a);
}
public static void RunOrThrow<T>(this Maybe<T> m, Action<T> fn, Exception e = null) {
if (!m.HasValue) {
throw e ?? new InvalidOperationException("RunOrThrow on Maybe threw the default exception");
}
fn(m.Value);
}
public static void RunWhenTrue(this Maybe<bool> m, Action fn) {
if (m.HasValue && m.Value)
fn();
}
public static Maybe<T> Collapse<T>(this Maybe<Maybe<T>> t) {
if (t.IsNothing() || t.Value.IsNothing())
return Maybe<T>.Nothing;
return t.Value;
}
public static bool IsSomething<T>(this Maybe<T> a) {
return a.HasValue;
}
public static bool IsNothing<T>(this Maybe<T> a) {
return !a.IsSomething();
}
public static Maybe<T2> Compose<T, T2>(this Maybe<T> a, Maybe<T2> b) {
if (a.IsNothing())
return Maybe<T2>.Nothing;
return b;
}
public static Maybe<T> Or<T>(this Maybe<T> a, T b) {
if (a.IsSomething())
return a;
return b.ToMaybe();
}
public static Maybe<T> Or<T>(this Maybe<T> a, Func<Maybe<T>> b) {
if (a.IsSomething())
return a;
return b();
}
public static Maybe<T> Or<T>(this Maybe<T> a, Maybe<T> b) {
if (a.IsSomething())
return a;
return b;
}
public static void Run<T, T2, T3>(this Maybe<T> m, Maybe<T2> m2, Maybe<T3> m3, Action<T, T2, T3> fn) {
if (m.IsSomething() && m2.IsSomething() && m3.IsSomething())
fn(m.Value, m2.Value, m3.Value);
}
public static void Run<T, T2>(this Maybe<T> m, Maybe<T2> m2, Action<T, T2> fn) {
if (m.IsSomething() && m2.IsSomething())
fn(m.Value, m2.Value);
}
public static void Run<T>(this Maybe<T> m, Action<T> fn) {
if (m.IsSomething())
fn(m.Value);
}
public static Maybe<T> ToMaybeFromList<T>(this IEnumerable<T> xs) {
foreach (var x in xs) {
return x.ToMaybe();
}
return Maybe<T>.Nothing;
}
public static Maybe<TResult> Select<T, TResult>(this Maybe<T> m, Func<T, TResult> fn) {
return m.HasValue ? new Maybe<TResult>(fn(m.Value)) : Maybe<TResult>.Nothing;
}
public static IEnumerable<Maybe<TResult>> Select<T, TResult>(this IEnumerable<Maybe<T>> maybes, Func<T, TResult> selector) {
return maybes.Select(maybe => maybe.Select(selector));
}
public static IEnumerable<T> SelectValid<T>(this IEnumerable<Maybe<T>> maybes) {
return SelectValid(maybes, m => m);
}
public static IEnumerable<TResult> SelectValid<T, TResult>(this IEnumerable<Maybe<T>> maybes, Func<T, TResult> fn) {
foreach (var maybe in maybes) {
if (maybe.HasValue)
yield return fn(maybe.Value);
}
}
public static Maybe<IEnumerable<T>> WhereMaybe<T>(this IEnumerable<T> xs, Func<T, Maybe<bool>> pred) {
var l = new List<T>();
foreach (var x in xs) {
var r = pred(x);
if (!r.HasValue)
return Maybe<IEnumerable<T>>.Nothing;
if (r.Value)
l.Add(x);
}
return new Maybe<IEnumerable<T>>(l);
}
public static Maybe<T> Where<T>(this Maybe<T> maybe, Func<T, bool> cond) {
if (!maybe.HasValue)
return maybe;
if (cond(maybe.Value))
return maybe;
return Maybe<T>.Nothing;
}
public static bool AnyNothing<T>(this IEnumerable<Maybe<T>> maybes) {
return maybes.Any(m => !m.HasValue);
}
public static Maybe<IEnumerable<T>> Sequence<T>(this IEnumerable<Maybe<T>> maybes) {
// there has got to be a better way to do this
if (maybes.AnyNothing())
return Maybe<IEnumerable<T>>.Nothing;
return maybes.Select(m => m.Value).ToMaybe();
}
public static Maybe<U> SelectMany<T, U>(this Maybe<T> m, Func<T, Maybe<U>> k) {
if (!m.HasValue)
return Maybe<U>.Nothing;
return k(m.Value);
}
public static Maybe<V> SelectMany<T, U, V>(this Maybe<T> m, Func<T, Maybe<U>> k, Func<T, U, V> s) {
return m.SelectMany(x => k(x).SelectMany(y => s(x, y).ToMaybe()));
}
}
}