-
-
Notifications
You must be signed in to change notification settings - Fork 417
/
IO.cs
1073 lines (928 loc) · 39.5 KB
/
IO.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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using LanguageExt.Common;
using LanguageExt.Traits;
namespace LanguageExt;
/// <summary>
/// A value of type `IO` a is a computation which, when performed, does some I/O before returning
/// a value of type `A`.
///
/// There is really only one way you should _"perform"_ an I/O action: bind it to `Main` in your
/// program: When your program is run, the I/O will be performed. It shouldn't be possible to
/// perform I/O from an arbitrary function, unless that function is itself in the `IO` monad and
/// called at some point, directly or indirectly, from `Main`.
///
/// Obviously, as this is C#, the above restrictions are for you to enforce. It would be reasonable
/// to relax that approach and have I/O invoked from, say, web-request handlers - or any other 'edges'
/// of your application.
///
/// `IO` is a monad, so `IO` actions can be combined using either the LINQ-notation or the `bind`
/// operations from the `Monad` class.
/// </summary>
/// <param name="runIO">The lifted thunk that is the IO operation</param>
/// <typeparam name="A">Bound value</typeparam>
public record IO<A>(Func<EnvIO, A> runIO) : K<IO, A>, Monoid<IO<A>>
{
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Construction
//
public static IO<A> Pure(A value) =>
new(_ => value);
public static IO<A> Fail(Error value) =>
new(_ => value.Throw<A>());
public static readonly IO<A> Empty =
new(_ => throw new ManyExceptions([]));
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Lifting
//
public static IO<A> Lift(Func<A> f) =>
new(_ => f());
public static IO<A> Lift(Func<EnvIO, A> f) =>
new(f);
public static IO<A> LiftAsync(Func<Task<A>> f) =>
new(env => Run(_ => f(), env));
public static IO<A> LiftAsync(Func<EnvIO, Task<A>> f) =>
new(env => Run(f, env));
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Functor
//
public IO<B> Map<B>(Func<A, B> f) =>
new(e =>
{
if (e.Token.IsCancellationRequested) throw new TaskCanceledException();
return f(Run(e));
});
public IO<A> MapFail(Func<Error, Error> f) =>
new(e =>
{
if (e.Token.IsCancellationRequested) throw new TaskCanceledException();
try
{
return Run(e);
}
catch (Exception ex)
{
return f(ex).Throw<A>();
}
});
public IO<B> BiMap<B>(Func<A, B> Succ, Func<Error, Error> Fail) =>
new(e =>
{
if (e.Token.IsCancellationRequested) throw new TaskCanceledException();
try
{
return Succ(Run(e));
}
catch (Exception ex)
{
return Fail(ex).Throw<B>();
}
});
public IO<B> Match<B>(Func<A, B> Succ, Func<Error, B> Fail) =>
new(e =>
{
if (e.Token.IsCancellationRequested) throw new TaskCanceledException();
try
{
return Succ(Run(e));
}
catch (Exception ex)
{
return Fail(ex);
}
});
public IO<A> IfFail(Func<Error, A> Fail) =>
Match(Prelude.identity, Fail);
public IO<A> IfFail(A Fail) =>
Match(Prelude.identity, _ => Fail);
public IO<A> IfFail(Func<Error, IO<A>> Fail) =>
Match(IO.Pure, Fail).Flatten();
public IO<A> IfFail(IO<A> Fail) =>
Match(IO.Pure, _ => Fail).Flatten();
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Folding
//
public IO<S> Fold<S>(
Schedule schedule,
S initialState,
Func<S, A, S> folder) =>
FoldUntil(schedule, initialState, folder, predicate: _ => false);
public IO<S> Fold<S>(
S initialState,
Func<S, A, S> folder) =>
FoldUntil(Schedule.Forever, initialState, folder, predicate: _ => false);
public IO<S> FoldWhile<S>(
Schedule schedule,
S initialState,
Func<S, A, S> folder,
Func<S, bool> stateIs) =>
FoldUntil(schedule, initialState, folder, Prelude.not(stateIs));
public IO<S> FoldWhile<S>(
S initialState,
Func<S, A, S> folder,
Func<S, bool> stateIs) =>
FoldUntil(Schedule.Forever, initialState, folder, Prelude.not(stateIs));
public IO<S> FoldWhile<S>(
Schedule schedule,
S initialState,
Func<S, A, S> folder,
Func<A, bool> valueIs) =>
FoldUntil(schedule, initialState, folder, Prelude.not(valueIs));
public IO<S> FoldWhile<S>(
S initialState,
Func<S, A, S> folder,
Func<A, bool> valueIs) =>
FoldUntil(Schedule.Forever, initialState, folder, Prelude.not(valueIs));
public IO<S> FoldWhile<S>(
Schedule schedule,
S initialState,
Func<S, A, S> folder,
Func<(S State, A Value), bool> predicate) =>
FoldUntil(schedule, initialState, folder, Prelude.not(predicate));
public IO<S> FoldWhile<S>(
S initialState,
Func<S, A, S> folder,
Func<(S State, A Value), bool> predicate) =>
FoldUntil(Schedule.Forever, initialState, folder, Prelude.not(predicate));
public IO<S> FoldUntil<S>(
Schedule schedule,
S initialState,
Func<S, A, S> folder,
Func<S, bool> stateIs) =>
FoldUntil(schedule, initialState, folder, p => stateIs(p.State));
public IO<S> FoldUntil<S>(
S initialState,
Func<S, A, S> folder,
Func<S, bool> stateIs) =>
FoldUntil(Schedule.Forever, initialState, folder, p => stateIs(p.State));
public IO<S> FoldUntil<S>(
Schedule schedule,
S initialState,
Func<S, A, S> folder,
Func<A, bool> valueIs) =>
FoldUntil(schedule, initialState, folder, p => valueIs(p.Value));
public IO<S> FoldUntil<S>(
S initialState,
Func<S, A, S> folder,
Func<A, bool> valueIs) =>
FoldUntil(Schedule.Forever, initialState, folder, p => valueIs(p.Value));
public IO<S> FoldUntil<S>(
S initialState,
Func<S, A, S> folder,
Func<(S State, A Value), bool> predicate) =>
FoldUntil(Schedule.Forever, initialState, folder, predicate);
public IO<S> FoldUntil<S>(
Schedule schedule,
S initialState,
Func<S, A, S> folder,
Func<(S State, A Value), bool> predicate) =>
new(envIO =>
{
if (envIO.Token.IsCancellationRequested) throw new TaskCanceledException();
var r = Run(envIO);
var state = folder(initialState, r);
if (predicate((state, r))) return state;
var token = envIO.Token;
foreach (var delay in schedule.Run())
{
IO.yieldFor(delay, token);
r = Run(envIO);
state = folder(state, r);
if (predicate((state, r))) return state;
}
return state;
});
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Cross thread-context posting
//
/// <summary>
/// Make this IO computation run on the `SynchronizationContext` that was captured at the start
/// of the IO chain (i.e. the one embedded within the `EnvIO` environment that is passed through
/// all IO computations)
/// </summary>
public IO<A> Post() =>
new(env =>
{
if (env.Token.IsCancellationRequested) throw new TaskCanceledException();
if (env.SyncContext is null) return Run(env);
A? value = default;
Exception? error = default;
var waiting = true;
try
{
env.SyncContext.Post(_ =>
{
try
{
value = Run(env);
}
catch (Exception e)
{
error = e;
}
finally
{
waiting = false;
}
},
null);
}
catch(Exception)
{
waiting = false;
throw;
}
SpinWait sw = default;
while (waiting && !env.Token.IsCancellationRequested)
{
sw.SpinOnce();
}
if (env.Token.IsCancellationRequested) throw new TaskCanceledException();
if (error is not null) error.Rethrow<A>();
return value!;
});
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Monad
//
public IO<B> Bind<B>(Func<A, IO<B>> f) =>
new(e =>
{
if (e.Token.IsCancellationRequested) throw new TaskCanceledException();
return f(Run(e)).Run(e);
});
public IO<B> Bind<B>(Func<A, K<IO, B>> f) =>
new(e =>
{
if (e.Token.IsCancellationRequested) throw new TaskCanceledException();
return f(Run(e)).As().Run(e);
});
public IO<B> Bind<B>(Func<A, Pure<B>> f) =>
new(e =>
{
if (e.Token.IsCancellationRequested) throw new TaskCanceledException();
return f(Run(e)).Value;
});
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// LINQ
//
public IO<B> Select<B>(Func<A, B> f) =>
Map(f);
public IO<C> SelectMany<B, C>(Func<A, IO<B>> bind, Func<A, B, C> project) =>
Bind(x => bind(x).Map(y => project(x, y)));
public IO<C> SelectMany<B, C>(Func<A, Pure<B>> bind, Func<A, B, C> project) =>
Bind(x => bind(x).Map(y => project(x, y)));
public OptionT<M, C> SelectMany<M, B, C>(Func<A, OptionT<M, B>> bind, Func<A, B, C> project)
where M : Monad<M>, Alternative<M> =>
OptionT<M, A>.LiftIO(this).SelectMany(bind, project);
public TryT<M, C> SelectMany<M, B, C>(Func<A, TryT<M, B>> bind, Func<A, B, C> project)
where M : Monad<M>, Alternative<M> =>
TryT<M, A>.LiftIO(this).SelectMany(bind, project);
public EitherT<L, M, C> SelectMany<L, M, B, C>(Func<A, EitherT<L, M, B>> bind, Func<A, B, C> project)
where M : Monad<M>, Alternative<M> =>
EitherT<L, M, A>.LiftIO(this).SelectMany(bind, project);
public ValidationT<F, M, C> SelectMany<F, M, B, C>(Func<A, ValidationT<F, M, B>> bind, Func<A, B, C> project)
where F : Monoid<F>
where M : Monad<M>, Alternative<M> =>
ValidationT<F, M, A>.LiftIO(this).SelectMany(bind, project);
public ReaderT<Env, M, C> SelectMany<Env, M, B, C>(Func<A, ReaderT<Env, M, B>> bind, Func<A, B, C> project)
where M : Monad<M>, Alternative<M> =>
ReaderT<Env, M, A>.LiftIO(this).SelectMany(bind, project);
public StateT<S, M, C> SelectMany<S, M, B, C>(Func<A, StateT<S, M, B>> bind, Func<A, B, C> project)
where M : Monad<M>, Alternative<M> =>
StateT<S, M, A>.LiftIO(this).SelectMany(bind, project);
public Eff<C> SelectMany<B, C>(Func<A, Eff<B>> bind, Func<A, B, C> project) =>
Eff<A>.LiftIO(this).SelectMany(bind, project);
public Eff<RT, C> SelectMany<RT, B, C>(Func<A, Eff<RT, B>> bind, Func<A, B, C> project) =>
Eff<RT, A>.LiftIO(this).SelectMany(bind, project);
public IO<C> SelectMany<C>(Func<A, Guard<Error, Unit>> bind, Func<A, Unit, C> project) =>
SelectMany(a => bind(a).ToIO(), project);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Fail coalescing
//
public IO<A> Or(IO<A> mb) =>
new(e =>
{
if (e.Token.IsCancellationRequested) throw new TaskCanceledException();
var lenv = e.LocalResources;
try
{
var r = Run(lenv);
e.Resources.Merge(lenv.Resources);
return r;
}
catch
{
lenv.Resources.ReleaseAll().Run(e);
return mb.Run(e);
}
});
public static IO<A> operator |(IO<A> ma, IO<A> mb) =>
ma.Or(mb);
public static IO<A> operator |(IO<A> ma, Pure<A> mb) =>
ma.Or(mb);
public static IO<A> operator |(IO<A> ma, Fail<Error> mb) =>
ma.Or(mb);
public static IO<A> operator |(IO<A> ma, Fail<Exception> mb) =>
ma.Or(mb);
public static IO<A> operator |(IO<A> ma, Error mb) =>
ma.Or(mb);
public static IO<A> operator |(IO<A> ma, CatchError mb) =>
ma | mb.As();
public static IO<A> operator |(IO<A> ma, CatchValue<A> mb) =>
ma | mb.As();
public static IO<A> operator |(IO<A> ma, CatchIO<A> mb) =>
new(env =>
{
if (env.Token.IsCancellationRequested) throw new TaskCanceledException();
try
{
return ma.Run(env);
}
catch (Exception ex)
{
var err = Error.New(ex);
if(mb.Match(err)) return mb.Value(err).Run(env);
throw;
}
});
public static IO<A> operator |(IO<A> ma, CatchM<IO, A> mb) =>
new(env =>
{
if (env.Token.IsCancellationRequested) throw new TaskCanceledException();
try
{
return ma.Run(env);
}
catch (Exception ex)
{
var err = Error.New(ex);
if(mb.Match(err)) return mb.Value(err).As().Run(env);
throw;
}
});
public static IO<A> operator |(IO<A> ma, CatchError<Error> mb) =>
new(env =>
{
if (env.Token.IsCancellationRequested) throw new TaskCanceledException();
try
{
return ma.Run(env);
}
catch (Exception ex)
{
var err = Error.New(ex);
if(mb.Match(err)) return mb.Value(err).Throw<A>();
throw;
}
});
public static IO<A> operator |(IO<A> ma, CatchError<Exception> mb) =>
new(env =>
{
if (env.Token.IsCancellationRequested) throw new TaskCanceledException();
try
{
return ma.Run(env);
}
catch (Exception ex)
{
if(mb.Match(ex)) return mb.Value(ex).Rethrow<A>();
throw;
}
});
public static IO<A> operator |(IO<A> ma, CatchValue<Error, A> mb) =>
new(env =>
{
if (env.Token.IsCancellationRequested) throw new TaskCanceledException();
try
{
return ma.Run(env);
}
catch (Exception ex)
{
var err = Error.New(ex);
if(mb.Match(err)) return mb.Value(err);
throw;
}
});
public static IO<A> operator |(IO<A> ma, CatchValue<Exception, A> mb) =>
new(env =>
{
if (env.Token.IsCancellationRequested) throw new TaskCanceledException();
try
{
return ma.Run(env);
}
catch (Exception ex)
{
if(mb.Match(ex)) return mb.Value(ex);
throw;
}
});
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Brackets
//
/// <summary>
/// The IO monad tracks resources automatically, this creates a local resource environment
/// to run this computation in. Once the computation has completed any resources acquired
/// are automatically released. Imagine this as the ultimate `using` statement.
/// </summary>
[Pure]
[MethodImpl(Opt.Default)]
public IO<A> Bracket() =>
new(env =>
{
using var lenv = env.LocalResources;
return Run(lenv);
});
/// <summary>
/// When acquiring, using, and releasing various resources, it can be quite convenient to write a function to manage
/// the acquisition and releasing, taking a function of the acquired value that specifies an action to be performed
/// in between.
/// </summary>
/// <param name="Use">Function to use the acquired resource</param>
/// <param name="Finally">Function to invoke to release the resource</param>
public IO<C> Bracket<B, C>(Func<A, IO<C>> Use, Func<A, IO<B>> Finally) =>
new(env =>
{
var x = Run(env);
try
{
return Use(x).Run(env);
}
finally
{
Finally(x).Run(env)?.Ignore();
}
});
/// <summary>
/// When acquiring, using, and releasing various resources, it can be quite convenient to write a function to manage
/// the acquisition and releasing, taking a function of the acquired value that specifies an action to be performed
/// in between.
/// </summary>
/// <param name="Use">Function to use the acquired resource</param>
/// <param name="Catch">Function to run to handle any exceptions</param>
/// <param name="Finally">Function to invoke to release the resource</param>
public IO<C> Bracket<B, C>(Func<A, IO<C>> Use, Func<Error, IO<C>> Catch, Func<A, IO<B>> Finally) =>
new(env =>
{
var x = Run(env);
try
{
return Use(x).Run(env);
}
catch (Exception e)
{
return Catch(e).Run(env);
}
finally
{
Finally(x).Run(env)?.Ignore();
}
});
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Trait implementation
//
IO<A> Semigroup<IO<A>>.Combine(IO<A> y) =>
this | y;
static IO<A> Monoid<IO<A>>.Empty =>
Empty;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Conversion
//
public static implicit operator IO<A>(Pure<A> ma) =>
Pure(ma.Value);
public static implicit operator IO<A>(Error error) =>
Lift(error.Throw<A>);
public static implicit operator IO<A>(Fail<Error> ma) =>
Lift(() => ma.Value.Throw<A>());
public static implicit operator IO<A>(Fail<Exception> ma) =>
Lift(() => ma.Value.Rethrow<A>());
public static implicit operator IO<A>(Lift<EnvIO, A> ma) =>
Lift(ma.Function);
public static implicit operator IO<A>(Lift<A> ma) =>
Lift(ma.Function);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Parallel
//
/// <summary>
/// Applies a time limit to the IO computation. If exceeded an exception is thrown.
/// </summary>
/// <param name="timeout">Timeout</param>
/// <returns>Result of the operation or throws if the time limit exceeded.</returns>
public IO<A> Timeout(TimeSpan timeout) =>
Fork(timeout).Await();
/// <summary>
/// Queues the specified work to run on the thread pool
/// </summary>
/// <remarks>
/// Any resources acquired within a forked IO computation will automatically be released upon the forked
/// computation's completion (successful or otherwise). Resources acquired in the parent thread will be
/// available to the forked thread, and can be released from there, but they are shared resources at that
/// point and should be treated with care.
/// </remarks>
/// <param name="timeout">Optional timeout</param>
/// <returns>`Fork` record that contains members for cancellation and optional awaiting</returns>
public IO<ForkIO<A>> Fork(Option<TimeSpan> timeout = default) =>
IO<ForkIO<A>>.Lift(
env =>
{
if (env.Token.IsCancellationRequested) throw new TaskCanceledException();
// Create a new local token-source with its own cancellation token
var tsrc = timeout.IsSome
? new CancellationTokenSource((TimeSpan)timeout)
: new CancellationTokenSource();
var token = tsrc.Token;
// If the parent cancels, we should too
var reg = env.Token.Register(() => tsrc.Cancel());
// Run the transducer asynchronously
var cleanup = new CleanUp(tsrc, reg);
var parentResources = env.Resources;
var task = Task.Run(
() =>
{
var forkedResources = new Resources(parentResources);
try
{
return runIO(EnvIO.New(forkedResources, token, tsrc, env.SyncContext));
}
finally
{
forkedResources.Dispose();
cleanup.Dispose();
}
}, token);
return new ForkIO<A>(
IO<Unit>.Lift(() => { tsrc.Cancel(); return default; }),
Lift(e => AwaitTask(task, e, token)));
});
/// <summary>
/// Run the `IO` monad to get its result
/// </summary>
/// <remarks>
/// Any lifted asynchronous operations will yield to the thread-scheduler, allowing other queued
/// operations to run concurrently. So, even though this call isn't awaitable it still plays
/// nicely and doesn't block the thread.
/// </remarks>
/// <remarks>
/// NOTE: An exception will always be thrown if the IO operation fails. Lift this monad into
/// other error handling monads to leverage more declarative error handling.
/// </remarks>
/// <returns>Result of the IO operation</returns>
/// <exception cref="TaskCanceledException">Throws if the operation is cancelled</exception>
/// <exception cref="BottomException">Throws if any lifted task fails without a value `Exception` value.</exception>
public A Run() =>
Run(EnvIO.New());
/// <summary>
/// Run the `IO` monad to get its result
/// </summary>
/// <remarks>
/// Any lifted asynchronous operations will yield to the thread-scheduler, allowing other queued
/// operations to run concurrently. So, even though this call isn't awaitable it still plays
/// nicely and doesn't block the thread.
/// </remarks>
/// <remarks>
/// NOTE: An exception will always be thrown if the IO operation fails. Lift this monad into
/// other error handling monads to leverage more declarative error handling.
/// </remarks>
/// <param name="env">IO environment</param>
/// <returns>Result of the IO operation</returns>
/// <exception cref="TaskCanceledException">Throws if the operation is cancelled</exception>
/// <exception cref="BottomException">Throws if any lifted task fails without a value `Exception` value.</exception>
public A Run(EnvIO env) =>
runIO(env);
/// <summary>
/// Run the `IO` monad to get its result. Differs from `Run` in that it catches any exceptions and turns
/// them into a `Fin<A>` result.
/// </summary>
public IO<Fin<A>> Try() =>
new(env =>
{
try
{
return Run(env);
}
catch (Exception e)
{
return (Error)e;
}
});
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Repeating the effect
//
/// <summary>
/// Keeps repeating the computation forever, or until an error occurs
/// </summary>
/// <remarks>
/// Any resources acquired within a repeated IO computation will automatically be released. This also means you can't
/// acquire resources and return them from within a repeated computation.
/// </remarks>
/// <returns>The result of the last invocation</returns>
public IO<A> Repeat() =>
RepeatUntil(Schedule.Forever, _ => false);
/// <summary>
/// Keeps repeating the computation, until the scheduler expires, or an error occurs
/// </summary>
/// <remarks>
/// Any resources acquired within a repeated IO computation will automatically be released. This also means you can't
/// acquire resources and return them from within a repeated computation.
/// </remarks>
/// <param name="schedule">Scheduler strategy for repeating</param>
/// <returns>The result of the last invocation</returns>
public IO<A> Repeat(Schedule schedule) =>
RepeatUntil(schedule, _ => false);
/// <summary>
/// Keeps repeating the computation until the predicate returns false, or an error occurs
/// </summary>
/// <remarks>
/// Any resources acquired within a repeated IO computation will automatically be released. This also means you can't
/// acquire resources and return them from within a repeated computation.
/// </remarks>
/// <param name="predicate">Keep repeating while this predicate returns `true` for each computed value</param>
/// <returns>The result of the last invocation</returns>
public IO<A> RepeatWhile(Func<A, bool> predicate) =>
RepeatUntil(Schedule.Forever, Prelude.not(predicate));
/// <summary>
/// Keeps repeating the computation, until the scheduler expires, or the predicate returns false, or an error occurs
/// </summary>
/// <remarks>
/// Any resources acquired within a repeated IO computation will automatically be released. This also means you can't
/// acquire resources and return them from within a repeated computation.
/// </remarks>
/// <param name="schedule">Scheduler strategy for repeating</param>
/// <param name="predicate">Keep repeating while this predicate returns `true` for each computed value</param>
/// <returns>The result of the last invocation</returns>
public IO<A> RepeatWhile(
Schedule schedule,
Func<A, bool> predicate) =>
RepeatUntil(schedule, Prelude.not(predicate));
/// <summary>
/// Keeps repeating the computation until the predicate returns true, or an error occurs
/// </summary>
/// <remarks>
/// Any resources acquired within a repeated IO computation will automatically be released. This also means you can't
/// acquire resources and return them from within a repeated computation.
/// </remarks>
/// <param name="predicate">Keep repeating until this predicate returns `true` for each computed value</param>
/// <returns>The result of the last invocation</returns>
public IO<A> RepeatUntil(
Func<A, bool> predicate) =>
RepeatUntil(Schedule.Forever, predicate);
/// <summary>
/// Keeps repeating the computation, until the scheduler expires, or the predicate returns true, or an error occurs
/// </summary>
/// <remarks>
/// Any resources acquired within a repeated IO computation will automatically be released. This also means you can't
/// acquire resources and return them from within a repeated computation.
/// </remarks>
/// <param name="schedule">Scheduler strategy for repeating</param>
/// <param name="predicate">Keep repeating until this predicate returns `true` for each computed value</param>
/// <returns>The result of the last invocation</returns>
public IO<A> RepeatUntil(
Schedule schedule,
Func<A, bool> predicate) =>
new(env =>
{
if (env.Token.IsCancellationRequested) throw new TaskCanceledException();
var token = env.Token;
var lenv = env.LocalResources;
try
{
var result = Run(lenv);
// free any resources acquired during a repeat
lenv.Resources.ReleaseAll().Run(env);
if (predicate(result)) return result;
foreach (var delay in schedule.Run())
{
IO.yieldFor(delay, token);
result = Run(lenv);
// free any resources acquired during a repeat
lenv.Resources.ReleaseAll().Run(env);
if (predicate(result)) return result;
}
return result;
}
finally
{
// free any resources acquired during a repeat
lenv.Resources.ReleaseAll().Run(env);
}
});
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Retrying the effect when it fails
//
/// <summary>
/// Retry if the IO computation fails
/// </summary>
/// <remarks>
/// This variant will retry forever
/// </remarks>
/// <remarks>
/// Any resources acquired within a retrying IO computation will automatically be released *if* the operation fails.
/// So, successive retries will not grow the acquired resources on each retry iteration. Any successful operation that
/// acquires resources will have them tracked in the usual way.
/// </remarks>
public IO<A> Retry() =>
RetryUntil(Schedule.Forever, _ => false);
/// <summary>
/// Retry if the IO computation fails
/// </summary>
/// <remarks>
/// This variant will retry until the schedule expires
/// </remarks>
/// <remarks>
/// Any resources acquired within a retrying IO computation will automatically be released *if* the operation fails.
/// So, successive retries will not grow the acquired resources on each retry iteration. Any successful operation that
/// acquires resources will have them tracked in the usual way.
/// </remarks>
public IO<A> Retry(Schedule schedule) =>
RetryUntil(schedule, _ => false);
/// <summary>
/// Retry if the IO computation fails
/// </summary>
/// <remarks>
/// This variant will keep retrying whilst the predicate returns `true` for the error generated at each iteration;
/// at which point the last raised error will be thrown.
/// </remarks>
/// <remarks>
/// Any resources acquired within a retrying IO computation will automatically be released *if* the operation fails.
/// So, successive retries will not grow the acquired resources on each retry iteration. Any successful operation that
/// acquires resources will have them tracked in the usual way.
/// </remarks>
public IO<A> RetryWhile(Func<Error, bool> predicate) =>
RetryUntil(Schedule.Forever, Prelude.not(predicate));
/// <summary>
/// Retry if the IO computation fails
/// </summary>
/// <remarks>
/// This variant will keep retrying whilst the predicate returns `true` for the error generated at each iteration;
/// or, until the schedule expires; at which point the last raised error will be thrown.
/// </remarks>
/// <remarks>
/// Any resources acquired within a retrying IO computation will automatically be released *if* the operation fails.
/// So, successive retries will not grow the acquired resources on each retry iteration. Any successful operation that
/// acquires resources will have them tracked in the usual way.
/// </remarks>
public IO<A> RetryWhile(
Schedule schedule,
Func<Error, bool> predicate) =>
RetryUntil(schedule, Prelude.not(predicate));
/// <summary>
/// Retry if the IO computation fails
/// </summary>
/// <remarks>
/// This variant will keep retrying until the predicate returns `true` for the error generated at each iteration;
/// at which point the last raised error will be thrown.
/// </remarks>
/// <remarks>
/// Any resources acquired within a retrying IO computation will automatically be released *if* the operation fails.
/// So, successive retries will not grow the acquired resources on each retry iteration. Any successful operation that
/// acquires resources will have them tracked in the usual way.
/// </remarks>
public IO<A> RetryUntil(
Func<Error, bool> predicate) =>
RetryUntil(Schedule.Forever, predicate);
/// <summary>
/// Retry if the IO computation fails
/// </summary>
/// <remarks>
/// This variant will keep retrying until the predicate returns `true` for the error generated at each iteration;
/// or, until the schedule expires; at which point the last raised error will be thrown.
/// </remarks>
/// <remarks>
/// Any resources acquired within a retrying IO computation will automatically be released *if* the operation fails.
/// So, successive retries will not grow the acquired resources on each retry iteration. Any successful operation that
/// acquires resources will have them tracked in the usual way.
/// </remarks>
public IO<A> RetryUntil(
Schedule schedule,
Func<Error, bool> predicate) =>
new(env =>
{
if (env.Token.IsCancellationRequested) throw new TaskCanceledException();
var token = env.Token;
var lastError = BottomException.Default as Exception;
var lenv = env.LocalResources;
try
{
var r = Run(lenv);
// Any resources that were acquired should be propagated through to the `env`
env.Resources.Merge(lenv.Resources);
return r;
}
catch (Exception e)
{
// Any resources created whilst trying should be removed for the retry
lenv.Resources.ReleaseAll().Run(env);
if (predicate(Error.New(e))) throw;
lastError = e;
}
foreach(var delay in schedule.Run())
{
IO.yieldFor(delay, token);
try
{
var r = Run(lenv);
// Any resources that were acquired should be propagated through to the `env`
env.Resources.Merge(lenv.Resources);
return r;
}
catch (Exception e)
{
// Any resources created whilst trying should be removed for the retry
lenv.Resources.ReleaseAll().Run(env);
if (predicate(Error.New(e))) throw;
lastError = e;
}
}
return lastError.Rethrow<A>();
});
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Internal
//
/// <summary>
/// Internal running of tasks without using the async/await machinery but still
/// yielding the thread for concurrency.
/// </summary>
/// <exception cref="TaskCanceledException"></exception>
/// <exception cref="BottomException"></exception>
static A Run(Func<EnvIO, Task<A>> runIO, EnvIO env)