public static Validation <MonoidFail, Fail, HashSet <B> > Traverse <MonoidFail, Fail, A, B>(this HashSet <Validation <MonoidFail, Fail, A> > ma, Func <A, B> f) where MonoidFail : struct, Monoid <Fail>, Eq <Fail> { var res = new B[ma.Count]; var errs = default(MonoidFail).Empty(); var isSuccess = true; var ix = 0; foreach (var x in ma) { if (x.IsFail) { errs = default(MonoidFail).Append(errs, x.FailValue); isSuccess = false; } else { res[ix] = f((A)x); ix++; } } return(isSuccess ? new HashSet <B>(res) : Validation <MonoidFail, Fail, HashSet <B> > .Fail(errs)); }
public Validation<FAIL, V> SelectMany<U, V>(Func<SUCCESS, Validation<FAIL, U>> bind, Func<SUCCESS, U, V> project) { var t = success; return IsSuccess ? bind(t).Map(u => project(t, u)) : Validation<FAIL, V>.Fail(FailValue); }
public static async ValueTask <Validation <Fail, B> > Traverse <Fail, A, B>(this Validation <Fail, ValueTask <A> > ma, Func <A, B> f) { if (ma.IsFail) { return(Validation <Fail, B> .Fail(ma.FailValue)); } return(Validation <Fail, B> .Success(f(await ma.SuccessValue.ConfigureAwait(false)))); }
public static async ValueTask <Validation <MonoidFail, Fail, B> > Traverse <MonoidFail, Fail, A, B>(this Validation <MonoidFail, Fail, ValueTask <A> > ma, Func <A, B> f) where MonoidFail : struct, Monoid <Fail>, Eq <Fail> { if (ma.IsFail) { return(Validation <MonoidFail, Fail, B> .Fail(ma.FailValue)); } return(Validation <MonoidFail, Fail, B> .Success(f(await ma.SuccessValue.ConfigureAwait(false)))); }
/// <summary> /// Last item in sequence. /// </summary> public Validation <F, A> LastOrInvalid <F>(Func <F> Fail) { if (seq != null) { Strict(); } return(IsEmpty ? Validation <F, A> .Fail(Seq1(Fail())) : Validation <F, A> .Success(data[start + count - 1])); }
/// <summary> /// Last item in sequence. /// </summary> public Validation <MonoidFail, F, A> LastOrInvalid <MonoidFail, F>(Func <F> Fail) where MonoidFail : struct, Monoid <F>, Eq <F> { if (seq != null) { Strict(); } return(IsEmpty ? Validation <MonoidFail, F, A> .Fail(Fail()) : Validation <MonoidFail, F, A> .Success(data[start + count - 1])); }
public static Validation <Fail, Identity <B> > Traverse <Fail, A, B>(this Identity <Validation <Fail, A> > ma, Func <A, B> f) { if (ma.Value.IsFail) { return(Validation <Fail, Identity <B> > .Fail((Seq <Fail>) ma.Value)); } else { return(Validation <Fail, Identity <B> > .Success(new Identity <B>(f((A)ma.Value)))); } }
public static Validation <MonoidFail, Fail, Identity <B> > Traverse <MonoidFail, Fail, A, B>(this Identity <Validation <MonoidFail, Fail, A> > ma, Func <A, B> f) where MonoidFail : struct, Monoid <Fail>, Eq <Fail> { if (ma.Value.IsFail) { return(Validation <MonoidFail, Fail, Identity <B> > .Fail(ma.Value.FailValue)); } else { return(Validation <MonoidFail, Fail, Identity <B> > .Success(Id <B>(f((A)ma.Value)))); } }
public static Validation <Fail, Try <B> > Traverse <Fail, A, B>(this Try <Validation <Fail, A> > ma, Func <A, B> f) { var tres = ma.Try(); if (tres.IsBottom || tres.IsFaulted) { return(Validation <Fail, Try <B> > .Success(TryFail <B>(tres.Exception))); } else if (tres.Value.IsFail) { return(Validation <Fail, Try <B> > .Fail((Seq <Fail>) tres.Value)); } else { return(Validation <Fail, Try <B> > .Success(Try(f((A)tres.Value)))); } }
public static Validation <Fail, Eff <B> > Traverse <Fail, A, B>(this Eff <Validation <Fail, A> > ma, Func <A, B> f) { var tres = ma.Run(); if (tres.IsBottom || tres.IsFail) { return(Validation <Fail, Eff <B> > .Success(FailEff <B>(tres.Error))); } else if (tres.Value.IsFail) { return(Validation <Fail, Eff <B> > .Fail((Seq <Fail>) tres.Value)); } else { return(Validation <Fail, Eff <B> > .Success(SuccessEff(f((A)tres.Value)))); } }
public static Validation <MonoidFail, Fail, Try <B> > Traverse <MonoidFail, Fail, A, B>(this Try <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, Try <B> > .Success(TryFail <B>(tres.Exception))); } else if (tres.Value.IsFail) { return(Validation <MonoidFail, Fail, Try <B> > .Fail(tres.Value.FailValue)); } else { return(Validation <MonoidFail, Fail, Try <B> > .Success(Try(f((A)tres.Value)))); } }
public static Validation <MonoidFail, Fail, Eff <B> > Traverse <MonoidFail, Fail, A, B>(this Eff <Validation <MonoidFail, Fail, A> > ma, Func <A, B> f) where MonoidFail : struct, Monoid <Fail>, Eq <Fail> { var tres = ma.Run(); if (tres.IsBottom || tres.IsFail) { return(Validation <MonoidFail, Fail, Eff <B> > .Success(FailEff <B>(tres.Error))); } else if (tres.Value.IsFail) { return(Validation <MonoidFail, Fail, Eff <B> > .Fail(tres.Value.FailValue)); } else { return(Validation <MonoidFail, Fail, Eff <B> > .Success(SuccessEff(f((A)tres.Value)))); } }
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)); } } }
public static EitherUnsafe <L, Validation <Fail, B> > Traverse <Fail, L, A, B>(this Validation <Fail, EitherUnsafe <L, A> > ma, Func <A, B> f) { if (ma.IsFail) { return(Right(Validation <Fail, B> .Fail(ma.FailValue))); } else { var mb = ma.SuccessValue; if (mb.IsLeft) { return(EitherUnsafe <L, Validation <Fail, B> > .Left((L)mb)); } else { return(EitherUnsafe <L, Validation <Fail, B> > .Right(f((A)mb))); } } }
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))); } } }
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))); } } }
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))); } } }
public static EitherUnsafe <Fail, Validation <MonoidFail, Fail, B> > Traverse <MonoidFail, Fail, A, B>(this Validation <MonoidFail, Fail, EitherUnsafe <Fail, A> > ma, Func <A, B> f) where MonoidFail : struct, Monoid <Fail>, Eq <Fail> { if (ma.IsFail) { return(Right(Validation <MonoidFail, Fail, B> .Fail(ma.FailValue))); } else { var mb = ma.SuccessValue; if (mb.IsLeft) { return(EitherUnsafe <Fail, Validation <MonoidFail, Fail, B> > .Left((Fail)mb)); } else { return(EitherUnsafe <Fail, Validation <MonoidFail, Fail, B> > .Right(f((A)mb))); } } }
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))); } } }
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)); } } }
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))); } } }
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()))); }
public static Validation <Fail, Stck <B> > Traverse <Fail, A, B>(this Stck <Validation <Fail, A> > ma, Func <A, B> f) { var res = new B[ma.Count]; var errs = new List <Fail>(); var ix = ma.Count - 1; foreach (var x in ma) { if (x.IsFail) { errs.AddRange((Seq <Fail>)x); } else { res[ix] = f((A)x); ix--; } } return(errs.Count == 0 ? new Stck <B>(res) : Validation <Fail, Stck <B> > .Fail(Seq.FromArray(errs.ToArray()))); }
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)); }
public static HashSet <Validation <Fail, B> > Traverse <Fail, A, B>(this Validation <Fail, HashSet <A> > ma, Func <A, B> f) => ma.Match( Fail: es => HashSet(Validation <Fail, B> .Fail(es)), Succ: xs => xs.Map(x => Success <Fail, B>(f(x))));
public static Lst <Validation <MonoidFail, Fail, B> > Traverse <MonoidFail, Fail, A, B>(this Validation <MonoidFail, Fail, Lst <A> > ma, Func <A, B> f) where MonoidFail : struct, Monoid <Fail>, Eq <Fail> => ma.Match( Fail : ex => List(Validation <MonoidFail, Fail, B> .Fail(ex)), Succ : xs => xs.Map(x => Success <MonoidFail, Fail, B>(f(x))));
public static Lst <Validation <Fail, B> > Traverse <Fail, A, B>(this Validation <Fail, Lst <A> > ma, Func <A, B> f) => ma.Match( Fail: ex => List(Validation <Fail, B> .Fail(ex)), Succ: xs => xs.Map(x => Success <Fail, B>(f(x))));
public static IEnumerable <Validation <MonoidFail, Fail, B> > Traverse <MonoidFail, Fail, A, B>(this Validation <MonoidFail, Fail, IEnumerable <A> > ma, Func <A, B> f) where MonoidFail : struct, Monoid <Fail>, Eq <Fail> => ma.Match( Fail : es => new[] { Validation <MonoidFail, Fail, B> .Fail(es) }, Succ: xs => xs.Map(x => Success <MonoidFail, Fail, B>(f(x))));
public Validation <MonoidFail, FAIL, U> Bind <U>(Func <SUCCESS, Validation <MonoidFail, FAIL, U> > f) => IsSuccess ? f(success) : Validation <MonoidFail, FAIL, U> .Fail(FailValue);
public static Stck <Validation <Fail, B> > Traverse <Fail, A, B>(this Validation <Fail, Stck <A> > ma, Func <A, B> f) => ma.Match( Fail: ex => Stack(Validation <Fail, B> .Fail(ex)), Succ: xs => toStack(xs.Map(x => Success <Fail, B>(f(x)))));