Exemplo n.º 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));
 };
Exemplo n.º 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());
 };
Exemplo n.º 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));
Exemplo n.º 4
0
 public static Exceptional <T> Try <T>(this Try <T> @this)
 {
     try { return(@this()); }
     catch (Exception e) { return(Exceptional.Of <T>(e)); }
 }
Exemplo n.º 5
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));
Exemplo n.º 6
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));
Exemplo n.º 7
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)));