Beispiel #1
0
        public static Try <C> SelectMany <A, B, C>(this Try <A> ta, Func <A, Try <B> > map, Func <A, B, C> selector)
        {
            try
            {
                if (ta.IsError)
                {
                    return(ta.AsError().Value);
                }

                var a = ta.AsSuccess().Value;

                var tb = map(a);

                if (tb.IsError)
                {
                    return(tb.AsError().Value);
                }

                var b = tb.AsSuccess().Value;

                return(selector(a, b));
            }
            catch (Exception e)
            {
                return(e);
            }
        }
Beispiel #2
0
 public static Result Else <A, Result>(this Try <A> either,
                                       Func <A, Result> happy,
                                       Func <Exception, Result> sad)
 {
     return(either.IsSuccess
         ? happy(either.AsSuccess().Value)
         : sad(either.AsError().Value));
 }
Beispiel #3
0
 public static Result Unify <Success, Result>(this Try <Success> either,
                                              Func <Success, Result> successFunc, Func <Exception, Result> errorFunc)
 {
     if (either.IsSuccess)
     {
         return(successFunc(either.AsSuccess().Value));
     }
     return(errorFunc(either.AsError().Value));
 }
Beispiel #4
0
        public static Try <A> WhenSuccess <A>(this Try <A> either,
                                              Action <A> callbackForSuccess)
        {
            if (either.IsSuccess)
            {
                callbackForSuccess(either.AsSuccess().Value);
            }

            return(either);
        }
Beispiel #5
0
        public static Success Else <Success>(this Try <Success> either,
                                             Func <Exception, Success> callback)
        {
            if (either.IsError)
            {
                return(callback(either.AsError().Value));
            }

            return(either.AsSuccess().Value);
        }
Beispiel #6
0
 public static Try <A> Where <A>(this Try <A> either,
                                 Predicate <A> predicate, Func <Exception> errorCallback)
 {
     if (either.IsError)
     {
         return(either);
     }
     if (predicate(either.AsSuccess().Value))
     {
         return(either);
     }
     return(errorCallback());
 }
Beispiel #7
0
        public static Try <B> SelectMany <A, B>(this Try <A> ta, Func <A, Try <B> > map)
        {
            try
            {
                if (ta.IsError)
                {
                    return(ta.AsError().Value);
                }

                var a = ta.AsSuccess().Value;

                return(map(a));
            }
            catch (Exception e)
            {
                return(e);
            }
        }