コード例 #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));
 };
コード例 #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());
 };
コード例 #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));
コード例 #4
0
 public static Exceptional <T> Try <T>(this Try <T> @this)
 {
     try { return(@this()); }
     catch (Exception e) { return(Exceptional.Of <T>(e)); }
 }
コード例 #5
0
 Exception ExceptionOrDefault(Exceptional <T> ex)
 => ex.Match(exc => exc, _ => default(Exception));
コード例 #6
0
 T ValueOrDefault(Exceptional <T> ex)
 => ex.Match(exc => default(T), t => t);
コード例 #7
0
 bool IsSuccess(Exceptional <T> ex)
 => ex.Match(_ => false, _ => true);
コード例 #8
0
 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));
コード例 #9
0
 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));
コード例 #10
0
 // 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)));
コード例 #11
0
 T ExtractValue(Exceptional <T> ex)
 => ex.Match(_ => throw new InvalidOperationException(), t => t);