Ejemplo n.º 1
0
 public static Validation <Fail, Fin <B> > Traverse <Fail, A, B>(this Fin <Validation <Fail, A> > ma, Func <A, B> f)
 {
     if (ma.IsFail)
     {
         return(Validation <Fail, Fin <B> > .Success(ma.Cast <B>()));
     }
     else
     {
         var mb = (Validation <Fail, A>)ma;
         if (mb.IsFail)
         {
             return(Validation <Fail, Fin <B> > .Fail((Seq <Fail>) mb));
         }
         else
         {
             return(Validation <Fail, Fin <B> > .Success(f((A)mb)));
         }
     }
 }
Ejemplo n.º 2
0
 public static Validation <Fail, OptionUnsafe <B> > Traverse <Fail, A, B>(this OptionUnsafe <Validation <Fail, A> > ma, Func <A, B> f)
 {
     if (ma.IsNone)
     {
         return(Validation <Fail, OptionUnsafe <B> > .Success(OptionUnsafe <B> .None));
     }
     else
     {
         var mb = (Validation <Fail, A>)ma;
         if (mb.IsFail)
         {
             return(Validation <Fail, OptionUnsafe <B> > .Fail((Seq <Fail>) mb));
         }
         else
         {
             return(Validation <Fail, OptionUnsafe <B> > .Success(f((A)mb)));
         }
     }
 }
Ejemplo n.º 3
0
 public static Validation <Fail, EitherUnsafe <L, B> > Traverse <L, Fail, A, B>(this EitherUnsafe <L, Validation <Fail, A> > ma, Func <A, B> f)
 {
     if (ma.IsLeft)
     {
         return(Validation <Fail, EitherUnsafe <L, B> > .Success(ma.LeftValue));
     }
     else
     {
         var mb = (Validation <Fail, A>)ma;
         if (mb.IsFail)
         {
             return(Validation <Fail, EitherUnsafe <L, B> > .Fail((Seq <Fail>) mb));
         }
         else
         {
             return(Validation <Fail, EitherUnsafe <L, B> > .Success(f((A)mb)));
         }
     }
 }
Ejemplo n.º 4
0
 public static Validation <Fail, Validation <Fail, B> > Traverse <Fail, A, B>(this Validation <Fail, Validation <Fail, A> > ma, Func <A, B> f)
 {
     if (ma.IsFail)
     {
         return(Validation <Fail, Validation <Fail, B> > .Success(ma.FailValue));
     }
     else
     {
         var mb = ma.SuccessValue;
         if (mb.IsSuccess)
         {
             return(Validation <Fail, Validation <Fail, B> > .Success(f((A)mb)));
         }
         else
         {
             return(Validation <Fail, Validation <Fail, B> > .Fail((Seq <Fail>) mb));
         }
     }
 }
Ejemplo n.º 5
0
 public static Validation <MonoidFail, Fail, EitherUnsafe <Fail, B> > Traverse <MonoidFail, Fail, A, B>(this EitherUnsafe <Fail, Validation <MonoidFail, Fail, A> > ma, Func <A, B> f)
     where MonoidFail : struct, Monoid <Fail>, Eq <Fail>
 {
     if (ma.IsLeft)
     {
         return(Validation <MonoidFail, Fail, EitherUnsafe <Fail, B> > .Success(ma.LeftValue));
     }
     else
     {
         var mb = (Validation <MonoidFail, Fail, A>)ma;
         if (mb.IsFail)
         {
             return(Validation <MonoidFail, Fail, EitherUnsafe <Fail, B> > .Fail(mb.FailValue));
         }
         else
         {
             return(Validation <MonoidFail, Fail, EitherUnsafe <Fail, B> > .Success(f((A)mb)));
         }
     }
 }
Ejemplo n.º 6
0
 public static Validation <MonoidFail, Fail, Validation <MonoidFail, Fail, B> > Traverse <MonoidFail, Fail, A, B>(this Validation <MonoidFail, Fail, Validation <MonoidFail, Fail, A> > ma, Func <A, B> f)
     where MonoidFail : struct, Monoid <Fail>, Eq <Fail>
 {
     if (ma.IsFail)
     {
         return(Validation <MonoidFail, Fail, Validation <MonoidFail, Fail, B> > .Success(ma.FailValue));
     }
     else
     {
         var mb = ma.SuccessValue;
         if (mb.IsSuccess)
         {
             return(Validation <MonoidFail, Fail, Validation <MonoidFail, Fail, B> > .Success(f((A)mb)));
         }
         else
         {
             return(Validation <MonoidFail, Fail, Validation <MonoidFail, Fail, B> > .Fail(mb.FailValue));
         }
     }
 }
Ejemplo n.º 7
0
 public static Validation <MonoidFail, Fail, OptionUnsafe <B> > Traverse <MonoidFail, Fail, A, B>(this OptionUnsafe <Validation <MonoidFail, Fail, A> > ma, Func <A, B> f)
     where MonoidFail : struct, Monoid <Fail>, Eq <Fail>
 {
     if (ma.IsNone)
     {
         return(Validation <MonoidFail, Fail, OptionUnsafe <B> > .Success(OptionUnsafe <B> .None));
     }
     else
     {
         var mb = (Validation <MonoidFail, Fail, A>)ma;
         if (mb.IsFail)
         {
             return(Validation <MonoidFail, Fail, OptionUnsafe <B> > .Fail(mb.FailValue));
         }
         else
         {
             return(Validation <MonoidFail, Fail, OptionUnsafe <B> > .Success(f((A)mb)));
         }
     }
 }
Ejemplo n.º 8
0
 public static Try <Validation <Fail, B> > Traverse <Fail, A, B>(this Validation <Fail, Try <A> > ma, Func <A, B> f) => () =>
 {
     if (ma.IsFail)
     {
         return(new Result <Validation <Fail, B> >(Fail <Fail, B>(ma.FailValue)));
     }
     else
     {
         var mr = ma.SuccessValue();
         if (mr.IsBottom)
         {
             return(new Result <Validation <Fail, B> >(BottomException.Default));
         }
         if (mr.IsFaulted)
         {
             return(new Result <Validation <Fail, B> >(mr.Exception));
         }
         return(new Result <Validation <Fail, B> >(Validation <Fail, B> .Success(f(mr.Value))));
     }
 };
Ejemplo n.º 9
0
        public static Validation <Fail, TryOption <B> > Traverse <Fail, A, B>(this TryOption <Validation <Fail, A> > ma, Func <A, B> f)
        {
            var tres = ma.Try();

            if (tres.IsBottom || tres.IsFaulted)
            {
                return(Validation <Fail, TryOption <B> > .Success(TryOption <B>(tres.Exception)));
            }
            else if (tres.Value.IsNone)
            {
                return(Validation <Fail, TryOption <B> > .Success(TryOption <B>(None)));
            }
            else if (tres.Value.Value.IsFail)
            {
                return(Validation <Fail, TryOption <B> > .Fail((Seq <Fail>) tres.Value.Value));
            }
            else
            {
                return(Validation <Fail, TryOption <B> > .Success(TryOption(f((A)tres.Value.Value))));
            }
        }
Ejemplo n.º 10
0
        public static Validation <Fail, IEnumerable <B> > Traverse <Fail, A, B>(this IEnumerable <Validation <Fail, A> > ma, Func <A, B> f)
        {
            var res  = new List <B>();
            var errs = new List <Fail>();

            foreach (var x in ma)
            {
                if (x.IsFail)
                {
                    errs.AddRange((Seq <Fail>)x);
                }
                else
                {
                    res.Add(f((A)x));
                }
            }

            return(errs.Count == 0
                ? Validation <Fail, IEnumerable <B> > .Success(res)
                : Validation <Fail, IEnumerable <B> > .Fail(Seq.FromArray(errs.ToArray())));
        }
Ejemplo n.º 11
0
        public static Validation <MonoidFail, Fail, TryOption <B> > Traverse <MonoidFail, Fail, A, B>(this TryOption <Validation <MonoidFail, Fail, A> > ma, Func <A, B> f)
            where MonoidFail : struct, Monoid <Fail>, Eq <Fail>
        {
            var tres = ma.Try();

            if (tres.IsBottom || tres.IsFaulted)
            {
                return(Validation <MonoidFail, Fail, TryOption <B> > .Success(TryOptionFail <B>(tres.Exception)));
            }
            else if (tres.Value.IsNone)
            {
                return(Validation <MonoidFail, Fail, TryOption <B> > .Success(TryOption <B>(None)));
            }
            else if (tres.Value.Value.IsFail)
            {
                return(Validation <MonoidFail, Fail, TryOption <B> > .Fail(tres.Value.Value.FailValue));
            }
            else
            {
                return(Validation <MonoidFail, Fail, TryOption <B> > .Success(TryOption(f((A)tres.Value.Value))));
            }
        }
Ejemplo n.º 12
0
 public static TryOption <Validation <MonoidFail, Fail, B> > Traverse <MonoidFail, Fail, A, B>(
     this Validation <MonoidFail, Fail, TryOption <A> > ma, Func <A, B> f)
     where MonoidFail : struct, Monoid <Fail>, Eq <Fail> => () =>
 {
     if (ma.IsFail)
     {
         return(new OptionalResult <Validation <MonoidFail, Fail, B> >(Fail <MonoidFail, Fail, B>(ma.FailValue)));
     }
     var mr = ma.SuccessValue();
     if (mr.IsBottom)
     {
         return(new OptionalResult <Validation <MonoidFail, Fail, B> >(BottomException.Default));
     }
     if (mr.IsFaulted)
     {
         return(new OptionalResult <Validation <MonoidFail, Fail, B> >(mr.Exception));
     }
     if (mr.IsNone)
     {
         return(OptionalResult <Validation <MonoidFail, Fail, B> > .None);
     }
     return(new OptionalResult <Validation <MonoidFail, Fail, B> >(Validation <MonoidFail, Fail, B> .Success(f(mr.Value.Value))));
 };
Ejemplo n.º 13
0
        public static Validation <MonoidFail, Fail, IEnumerable <B> > Traverse <MonoidFail, Fail, A, B>(this IEnumerable <Validation <MonoidFail, Fail, A> > ma, Func <A, B> f)
            where MonoidFail : struct, Monoid <Fail>, Eq <Fail>
        {
            var res       = new List <B>();
            var errs      = default(MonoidFail).Empty();
            var isSuccess = true;

            foreach (var x in ma)
            {
                if (x.IsFail)
                {
                    errs      = default(MonoidFail).Append(errs, x.FailValue);
                    isSuccess = false;
                }
                else
                {
                    res.Add(f((A)x));
                }
            }

            return(isSuccess
                ? Validation <MonoidFail, Fail, IEnumerable <B> > .Success(res)
                : Validation <MonoidFail, Fail, IEnumerable <B> > .Fail(errs));
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Represents a successful operation
 /// </summary>
 /// <typeparam name="MonoidError">Monoid for collecting the errors</typeparam>
 /// <typeparam name="ERROR">Error type</typeparam>
 /// <typeparam name="A">Value type</typeparam>
 /// <param name="value">Value</param>
 /// <returns>Validation applicative</returns>
 public static Validation <MonoidError, ERROR, A> Success <MonoidError, ERROR, A>(A value)
     where MonoidError : struct, Monoid <ERROR>, Eq <ERROR> =>
 Validation <MonoidError, ERROR, A> .Success(value);
Ejemplo n.º 15
0
 /// <summary>
 /// Represents a successful operation
 /// </summary>
 /// <typeparam name="ERROR">Error type</typeparam>
 /// <typeparam name="A">Value type</typeparam>
 /// <param name="value">Value</param>
 /// <returns>Validation applicative</returns>
 public static Validation <ERROR, A> Success <ERROR, A>(A value) =>
 Validation <ERROR, A> .Success(value);