forked from noblethrasher/Prelude
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdHoc.cs
229 lines (187 loc) · 5.05 KB
/
AdHoc.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Prelude
{
public static class _Utils
{
public static ISet<T> CreateSet<T>(this IEqualityComparer<T> eq)
{
return new HashSet<T>(eq);
}
public static ISet<T> CreateSet<T>(this IEqualityComparer<T> eq, IEnumerable<T> xs)
{
return new HashSet<T>(xs, eq);
}
public static ISet<T> CreateSet<T>(this Func<T, T, bool> eq)
{
return new HashSet<T>(new _EqualityComparer<T>(eq, x => x.GetHashCode()));
}
public static ISet<T> AddRange<T>(this ISet<T> s, IEnumerable<T> xs)
{
if (xs != null)
foreach (var x in xs)
s.Add(x);
return s;
}
}
public class _Disposable : IDisposable
{
Action dispose;
public _Disposable(Action dispose)
{
this.dispose = dispose;
}
public void Dispose()
{
dispose();
}
}
public class _EqualityComparer<T> : IEqualityComparer<T>
{
Func<T, T, bool> equals;
Func<T, int> _getHashCode;
public _EqualityComparer(Func<T, T, bool> eq, Func<T, int> hashCode)
{
equals = eq;
_getHashCode = hashCode;
}
public bool Equals(T x, T y)
{
return equals(x, y);
}
public int GetHashCode(T obj)
{
return _getHashCode(obj);
}
}
public class _Equatable<T> : IEquatable<T>
{
readonly Func<T, bool> equals;
public _Equatable(Func<T, bool> eq)
{
equals = eq;
}
public bool Equals(T other)
{
return equals(other);
}
}
public class _Comparer<T> : IComparer<T>
{
readonly Func<T, T, int> comparer;
public _Comparer(Func<T, T, int> comparer)
{
this.comparer = comparer;
}
public int Compare(T x, T y)
{
return comparer(x, y);
}
}
public class _Comparable<T> : IComparable<T>
{
readonly Func<T, int> compare;
public _Comparable(Func<T, int> compare)
{
this.compare = compare;
}
public int CompareTo(T other)
{
return compare(other);
}
}
public class _Enumerator<T> : IEnumerator<T>
{
readonly Func<T> current;
readonly Action dispose, reset;
readonly Func<bool> moveNext;
public _Enumerator(Func<T> current, Func<bool> moveNext, Action dispose, Action reset)
{
this.current = current;
this.moveNext = moveNext;
this.dispose = dispose;
this.reset = reset;
}
public _Enumerator(Func<T> current, Func<bool> moveNext) : this(current, moveNext, null, null) { }
public T Current
{
get { return current(); }
}
public void Dispose()
{
if (dispose != null)
dispose();
}
object System.Collections.IEnumerator.Current
{
get { return this.Current; }
}
public bool MoveNext()
{
return this.moveNext();
}
public void Reset()
{
if (reset != null)
reset();
}
public IEnumerable<T> ToEnumerable()
{
return new _Enumerable<T>(() => this);
}
}
public class _Enumerable<T> : IEnumerable<T>
{
Func<IEnumerator<T>> _getEnumerator;
public _Enumerable(Func<IEnumerator<T>> getEnumerator)
{
_getEnumerator = getEnumerator;
}
public IEnumerator<T> GetEnumerator()
{
return _getEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public class _Observer<T> : IObserver<T>
{
Action onCompleted;
Action<Exception> error;
Action<T> onNext;
public _Observer(Action completed, Action<Exception> error, Action<T> next)
{
this.onCompleted = completed;
this.error = error;
this.onNext = next;
}
public void OnCompleted()
{
onCompleted();
}
public void OnError(Exception error)
{
this.error(error);
}
public void OnNext(T value)
{
onNext(value);
}
}
public class _Observable<T> : IObservable<T>
{
Func<IObserver<T>, IDisposable> subscribe;
public _Observable(Func<IObserver<T>, IDisposable> subscribe)
{
this.subscribe = subscribe;
}
public IDisposable Subscribe(IObserver<T> observer)
{
return subscribe(observer);
}
}
}