コード例 #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);
            }
        }
コード例 #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));
 }
コード例 #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));
 }
コード例 #4
0
        public static Try <A> WhenError <A>(this Try <A> either,
                                            Action <Exception> callbackForError)
        {
            if (either.IsError)
            {
                callbackForError(either.AsError().Value);
            }

            return(either);
        }
コード例 #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);
        }
コード例 #6
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);
            }
        }