Beispiel #1
0
 public static Try <R> Map <T, R>(this Try <T> @this, Func <T, R> mapper) => () =>
 {
     var result = @this.Try();
     return(result.Exception
      ? Exceptional.Of <R>(result.Ex)
      : mapper(result.Value));
 };
Beispiel #2
0
 public static Try <R> Bind <T, R>(this Try <T> @this, Func <T, Try <R> > binder) => () =>
 {
     var res = @this.Try();
     return(res.Exception
      ? Exceptional.Of <R>(res.Ex)
      : binder(res.Value).Try());
 };
Beispiel #3
0
        // Exceptional

        public static Exceptional <IEnumerable <R> > Traverse <T, R>(this IEnumerable <T> list
                                                                     , Func <T, Exceptional <R> > func)
        => list.Aggregate(
            seed: Exceptional.Of(Enumerable.Empty <R>()),
            // Exceptional<[R]> -> T -> Exceptional<[R]>
            func: (optRs, t) => from rs in optRs
            from r in func(t)
            select rs.Append(r));
Beispiel #4
0
 public static Exceptional <T> Try <T>(this Try <T> @this)
 {
     try { return(@this()); }
     catch (Exception e) { return(Exceptional.Of <T>(e)); }
 }
 Exception ExceptionOrDefault(Exceptional <T> ex)
 => ex.Match(exc => exc, _ => default(Exception));
 T ValueOrDefault(Exceptional <T> ex)
 => ex.Match(exc => default(T), t => t);
 bool IsSuccess(Exceptional <T> ex)
 => ex.Match(_ => false, _ => true);
 public static Validation <Exceptional <R> > Traverse <T, R>
     (this Exceptional <T> tr, Func <T, Validation <R> > f)
 => tr.Match(
     Exception: e => Valid(Exceptional.Of <R>(e)),
     Success: t => from r in f(t) select Exceptional.Of(r));
 public static Exceptional <Validation <R> > Traverse <T, R>
     (this Validation <T> tr, Func <T, Exceptional <R> > f)
 => tr.Match(
     Invalid: reasons => Exceptional.Of(Validation <R> .Fail(reasons)),
     Valid: t => from r in f(t) select Valid(r));
 // Exceptional
 public static Exceptional <Validation <R> > TraverseA <T, R>
     (this Validation <T> val, Func <T, Exceptional <R> > func)
 => val.Match(
     Invalid: reasons => Exceptional.Of(Validation <R> .Fail(reasons)),
     Valid: t => Exceptional.Of(Validation <R> .Return).Apply(func(t)));
 T ExtractValue(Exceptional <T> ex)
 => ex.Match(_ => throw new InvalidOperationException(), t => t);