-
-
Notifications
You must be signed in to change notification settings - Fork 424
/
Copy pathTESTING.cs
339 lines (269 loc) · 10.8 KB
/
TESTING.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
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
// TODO: Delete this or turn it into some real tests
using System;
using System.IO;
using System.Threading.Tasks;
using LanguageExt.Common;
using LanguageExt.Traits;
using static LanguageExt.Prelude;
namespace LanguageExt;
public static class Testing
{
public static void Test1()
{
var m1 = ReaderT<string>.lift(Some(123));
var m2 = ReaderT<string>.lift(Some(123));
var mt = ReaderT<string>.lift(Some(unit));
var m0 = from w in Pure(123)
from x in m1
from _ in unless(true, mt)
from y in m2
from z in asks((string env) => env.Length)
from e in ask<string>()
from n in ReaderT<string, Option, string>.Lift(Some("Paul"))
select $"{e} {n}: {w + x + y + z}";
var m3 = from w in Pure(123)
from x in m1
from y in m2
from z in Some(100)
from e in ask<string>()
select $"{e}: {w + x + y + z}";
var r1 = m3.As().Run("Hello");
var m4 = from x in m1
from y in m2
from e in ask<string>()
select $"{e}: {x + y}";
var r2 = m4.As().Run("Hello").As();
}
/*public static void Test2()
{
var m1 = Reader<string, int>.Pure(123);
var m2 = Reader<string, int>.Pure(123);
var m3 = from x in m1
from y in m2
from e in ask<string>()
select $"{e}: {x + y}";
var m4 = from x in m1
from y in m2
from e in ask<string>()
from z in Pure(234)
select $"{e}: {x + y}";
}*/
public static void Test3()
{
var m1 = ReaderT<string>.lift(Right<string, int>(123));
var m2 = ReaderT<string>.lift(Right<string, int>(123));
var m3 = from w in Pure(123)
from x in m1
from y in m2
from z in Right(100)
from e in ask<string>()
select $"{e}: {w + x + y + z}";
var r1 = m3.Run("Hello");
var m4 = from x in m1
from y in m2
from z in Left<string, int>("fail")
from e in ask<string>()
select $"{e}: {x + y + z}";
var r2 = m4.Run("Hello");
}
public static void Test4()
{
var m1 = App.Pure(123);
var m2 = App.Pure(123);
var m3 = App.Fail<int>(Error.New("fail"));
var m4 = from w in Pure(234)
from x in m1
from y in m2
from z in m3
from r in App.rootFolder
from t in liftIO(async () => await File.ReadAllTextAsync($"{r}\\test.txt"))
select $"{t}: {w + x + y + z}";
var r1 = m4.Run(new AppConfig("", "")).As();
}
public static void Test6()
{
var m1 = ReaderT<string>.lift(IdentityT<IO, int>.Lift(IO.pure(123)));
var m2 = ReaderT<string>.lift(IdentityT<IO, int>.Lift(IO.pure(123)));
var m0 = from w in Pure(123)
from p in ReaderT.ask<IdentityT<IO>, string>()
from x in IO.pure("Hello")
from i in ReaderT<string, IdentityT<IO>>.liftIO(IO.pure("Hello"))
from j in IO.pure("Hello").Fork()
from r in envIO
from y in m2
select $"{p} {y} {j}";
var value = m0.Run("Hello").As().Value.As().Run(EnvIO.New());
}
/*
public static void Test7()
{
var m1 = ResourceT.lift(ReaderT<string>.lift(IO.Pure(123)));
var m2 = ResourceT.lift(ReaderT<string>.lift(IO.Pure(123)));
var m0 = from w in Pure(123)
from q in m1
from f in ResourceT<ReaderT<string, IO>>.use(() => File.Open("c:\\test.txt", FileMode.Open))
from p in ReaderT.ask<IO, string>()
from x in IO.Pure("Hello")
from i in ReaderT<string, IO>.liftIO(IO.Pure("Hello"))
from j in IO.Pure("Hello").Fork()
from r in envIO
from y in m2
select $"{p} {y} {j}";
var value = m0.Run().As()
.Run("Hello").As();
}
public static void Test8()
{
var m1 = OptionT.lift(ReaderT<string>.lift(ResourceT.lift(IO.Pure(123))));
var m2 = OptionT.lift(ReaderT<string>.lift(ResourceT.lift(IO.Pure(123))));
var m0 = from w in Pure(123)
from q in m1
from f in use(() => File.Open("c:\\test.txt", FileMode.Open))
from p in ask<string>()
from i in liftIO(IO.Pure("Hello"))
from j in IO.Pure("Hello").Fork()
from r in envIO
from _ in release(f)
from y in m2
select $"{w} {f} {i}";
var value = m0.Match(Some: v => $"foo {v}",
None: () => "bar").As()
.Run("Paul").As()
.Run();
OptionT<ReaderT<Env, ResourceT<IO>>, Env> ask<Env>() =>
OptionT.lift(ReaderT.ask<ResourceT<IO>, Env>());
OptionT<ReaderT<string, ResourceT<IO>>, A> use<A>(Func<A> f) where A : IDisposable =>
OptionT.lift(ReaderT<string>.lift(ResourceT<IO>.use(f)));
OptionT<ReaderT<string, ResourceT<IO>>, Unit> release<A>(A value) where A : IDisposable =>
OptionT.lift(ReaderT<string>.lift(ResourceT<IO>.release(value)));
OptionT<ReaderT<string, ResourceT<IO>>, A> liftIO<A>(IO<A> ma) =>
OptionT.lift(ReaderT<string>.lift(ResourceT<IO>.liftIO(ma)));
}
*/
public static void Test9()
{
var m1 = OptionT.lift(StateT<string>.lift(IO.pure(100)));
var m2 = OptionT.lift(StateT<string>.lift(IO.pure(200)));
var m0 = from w in Pure(123)
from q in m1
from x in StateT.get<IO, string>()
from i in OptionT<StateT<string, IO>>.liftIO(IO.pure("Hello"))
from j in IO.pure("Hello").Fork()
from _ in StateT.put<IO, string>(x)
from r in envIO
from y in m2
select $"{w} {j} {i}";
var value = m0.Match(Some: v => $"foo {v}",
None: () => "bar").As()
.Run("Paul").As()
.Run();
}
public static void Test10()
{
var m1 = StateT<string>.lift(OptionT.lift(IO.pure(100)));
var m2 = StateT<string>.lift(OptionT.lift(IO.pure(200)));
var m0 = from w in Pure(123)
from q in m1
from x in StateT.get<OptionT<IO>, string>()
from i in StateT<string, OptionT<IO>>.liftIO(IO.pure("Hello"))
from j in IO.pure("Hello").Fork()
from _ in StateT.put<OptionT<IO>, string>(x)
from r in envIO
from y in m2
select $"{w} {j} {i}";
var value = m0.Run("Paul").As()
.Match(Some: v => $"value: {v.Value}, state: {v.State}",
None: () => "bar").As()
.Run();
}
static void TravTest1()
{
var xs = Seq(1, 2, 3, 4, 5);
var r1 = Traversable.traverse(x => Some(x), xs);
var ys = Seq(Some(1).Kind(), Some(2).Kind(), Option<int>.None.Kind(), Some(4).Kind(), Option<int>.None.Kind());
var r2 = Traversable.sequenceM(ys);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Generalised IO
//
public static class GeneralIO<M>
where M : Monad<M>
{
public static K<M, string> readAllText(string path) =>
M.LiftIO(liftIO(async _ => await File.ReadAllTextAsync(path)));
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Maybe test
//
public class Maybe : Monad<Maybe>
{
public static Maybe<A> Just<A>(A value) =>
new Just<A>(value);
public static K<Maybe, B> Bind<A, B>(K<Maybe, A> ma, Func<A, K<Maybe, B>> f) =>
ma.As().Bind(f);
public static K<Maybe, B> Map<A, B>(Func<A, B> f, K<Maybe, A> ma) =>
ma.As().Map(f);
public static K<Maybe, A> Pure<A>(A value) =>
Just(value);
public static K<Maybe, B> Apply<A, B>(K<Maybe, Func<A, B>> mf, K<Maybe, A> ma) =>
mf.As().Bind(f => ma.As().Map(f));
public static K<Maybe, B> Action<A, B>(K<Maybe, A> ma, K<Maybe, B> mb) =>
ma.As().Bind(_ => mb);
public static K<Maybe, A> LiftIO<A>(IO<A> ma) =>
throw new NotImplementedException();
}
public abstract record Maybe<A> : K<Maybe, A>
{
public static readonly Maybe<A> Nothing =
new Nothing<A>();
public abstract Maybe<B> Map<B>(Func<A, B> f);
public abstract Maybe<B> Bind<B>(Func<A, Maybe<B>> f);
public virtual Maybe<B> Bind<B>(Func<A, K<Maybe, B>> f) =>
Bind(x => f(x).As());
public Maybe<C> SelectMany<B, C>(Func<A, Maybe<B>> bind, Func<A, B, C> project) =>
Bind(x => bind(x).Map(y => project(x, y)));
public Maybe<C> SelectMany<B, C>(Func<A, K<Maybe, B>> bind, Func<A, B, C> project) =>
SelectMany(x => bind(x).As(), project);
}
public record Just<A>(A Value) : Maybe<A>
{
public override Maybe<B> Map<B>(Func<A, B> f) =>
new Just<B>(f(Value));
public override Maybe<B> Bind<B>(Func<A, Maybe<B>> f) =>
f(Value);
}
public record Nothing<A> : Maybe<A>
{
public override Maybe<B> Map<B>(Func<A, B> f) =>
Maybe<B>.Nothing;
public override Maybe<B> Bind<B>(Func<A, Maybe<B>> f) =>
Maybe<B>.Nothing;
}
public static class MaybeExt
{
public static Maybe<A> As<A>(this K<Maybe, A> ma) =>
(Maybe<A>)ma;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// App test
//
// Domain monad
public record App<A>(Func<AppConfig, K<Either<Error>, A>> runReader)
: ReaderT<AppConfig, Either<Error>, A>(runReader);
// Application environment
public record AppConfig(string ConnectionString, string RootFolder);
public static class App
{
public static App<A> Pure<A>(A value) =>
(App<A>)App<A>.Pure(value);
public static App<A> Fail<A>(Error error) =>
(App<A>)App<A>.Lift(Left<Error, A>(error));
public static App<string> connectionString =>
(App<string>)App<string>.Asks(env => env.ConnectionString);
public static App<string> rootFolder =>
(App<string>)App<string>.Asks(env => env.RootFolder);
}