public static Validation <MonoidFail, Fail, Seq <B> > Traverse <MonoidFail, Fail, A, B>(this Seq <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 ? Seq.FromArray(res) : Validation <MonoidFail, Fail, Seq <B> > .Fail(errs)); }
public static TryOptionAsync <Seq <B> > TraverseSerial <A, B>(this Seq <TryOptionAsync <A> > ma, Func <A, B> f) { return(ToTry(() => Go(ma, f))); async Task <OptionalResult <Seq <B> > > Go(Seq <TryOptionAsync <A> > ma, Func <A, B> f) { var rb = new List <B>(); foreach (var a in ma) { var mb = await a.Try().ConfigureAwait(false); if (mb.IsFaulted) { return(new OptionalResult <Seq <B> >(mb.Exception)); } if (mb.IsNone) { return(OptionalResult <Seq <B> > .None); } rb.Add(f(mb.Value.Value)); } return(new OptionalResult <Seq <B> >(Seq.FromArray(rb.ToArray()))); }; }
public static EitherAsync <L, Seq <B> > TraverseSerial <L, A, B>(this Seq <EitherAsync <L, A> > ma, Func <A, B> f) { return(new EitherAsync <L, Seq <B> >(Go(ma, f))); async Task <EitherData <L, Seq <B> > > Go(Seq <EitherAsync <L, A> > ma, Func <A, B> f) { var rb = new B[ma.Count]; var ix = 0; foreach (var a in ma) { var mb = await a; if (mb.IsBottom) { return(EitherData.Bottom <L, Seq <B> >()); } if (mb.IsLeft) { return(EitherData.Left <L, Seq <B> >(mb.LeftValue)); } rb[ix] = f(mb.RightValue); ix++; } return(EitherData.Right <L, Seq <B> >(Seq.FromArray <B>(rb))); }; }
public static TryOption <Seq <B> > Traverse <A, B>(this Seq <TryOption <A> > ma, Func <A, B> f) => () => { var res = new B[ma.Count]; var ix = 0; foreach (var xs in ma) { var x = xs(); if (x.IsBottom) { return(new OptionalResult <Seq <B> >(BottomException.Default)); } if (x.IsFaulted) { return(new OptionalResult <Seq <B> >(x.Exception)); } if (x.IsNone) { return(OptionalResult <Seq <B> > .None); } res[ix] = f(x.Value.Value); ix++; } return(new OptionalResult <Seq <B> >(Seq.FromArray(res))); };
public static async ValueTask <Seq <B> > TraverseSerial <A, B>(this Seq <ValueTask <A> > ma, Func <A, B> f) { var rb = new List <B>(); foreach (var a in ma) { rb.Add(f(await a.ConfigureAwait(false))); } return(Seq.FromArray <B>(rb.ToArray())); }
public static Identity <IEnumerable <B> > Traverse <A, B>(this IEnumerable <Identity <A> > ma, Func <A, B> f) { var res = new List <B>(); foreach (var xs in ma) { res.Add(f(xs.Value)); } return(new Identity <IEnumerable <B> >(Seq.FromArray(res.ToArray()))); }
public static Aff <RT, Seq <B> > TraverseParallel <RT, A, B>(this Seq <Aff <RT, A> > ma, Func <A, B> f, int windowSize) where RT : struct, HasCancel <RT> => AffMaybe <RT, Seq <B> >(async env => { var rs = await ma.AsEnumerable().Map(m => m.Run(env)).WindowMap(windowSize, fa => fa.Map(f)); var(fails, succs) = rs.Partition(); return(fails.Any() ? FinFail <Seq <B> >(fails.Head()) : FinSucc <Seq <B> >(Seq.FromArray(succs.ToArray()))); });
public static Aff <Seq <B> > TraverseParallel <A, B>(this Seq <Aff <A> > ma, Func <A, B> f, int windowSize) => AffMaybe <Seq <B> >(async() => { var rs = await ma.AsEnumerable().Map(m => m.Run()).WindowMap(windowSize, fa => fa.Map(f)).ConfigureAwait(false); var(fails, succs) = rs.Partition(); var fails1 = fails.Take(1).ToArray(); return(fails1.Length == 1 ? FinFail <Seq <B> >(fails1[0]) : FinSucc <Seq <B> >(Seq.FromArray(succs.ToArray()))); });
public static Identity <Seq <B> > Traverse <A, B>(this Seq <Identity <A> > ma, Func <A, B> f) { var res = new B[ma.Count]; var ix = 0; foreach (var xs in ma) { res[ix] = f(xs.Value); ix++; } return(new Identity <Seq <B> >(Seq.FromArray(res))); }
public static EitherAsync <L, Seq <B> > TraverseParallel <L, A, B>(this Seq <EitherAsync <L, A> > ma, int windowSize, Func <A, B> f) { return(new EitherAsync <L, Seq <B> >(Go(ma, f))); async Task <EitherData <L, Seq <B> > > Go(Seq <EitherAsync <L, A> > ma, Func <A, B> f) { var rb = await ma.Map(a => a.Map(f).Data).WindowMap(windowSize, identity).ConfigureAwait(false); return(rb.Exists(d => d.State == EitherStatus.IsLeft) ? rb.Filter(d => d.State == EitherStatus.IsLeft).Map(d => EitherData.Left <L, Seq <B> >(d.Left)).Head() : EitherData.Right <L, Seq <B> >(Seq.FromArray(rb.Map(d => d.Right).ToArray()))); } }
public static TryAsync <Seq <B> > TraverseParallel <A, B>(this Seq <TryAsync <A> > ma, int windowSize, Func <A, B> f) { return(ToTry(() => Go(ma, f))); async Task <Result <Seq <B> > > Go(Seq <TryAsync <A> > ma, Func <A, B> f) { var rb = await ma.Map(a => a.Map(f).Try()).WindowMap(windowSize, Prelude.identity).ConfigureAwait(false); return(rb.Exists(d => d.IsFaulted) ? rb.Filter(b => b.IsFaulted).Map(b => new Result <Seq <B> >(b.Exception)).Head() : new Result <Seq <B> >(Seq.FromArray(rb.Map(d => d.Value).ToArray()))); } }
public static Fin <IEnumerable <B> > Traverse <A, B>(this IEnumerable <Fin <A> > ma, Func <A, B> f) { var res = new List <B>(); foreach (var xs in ma) { if (xs.IsFail) { return(xs.Cast <IEnumerable <B> >()); } res.Add(f(xs.Value)); } return(Fin <IEnumerable <B> > .Succ(Seq.FromArray <B>(res.ToArray()))); }
public static OptionUnsafe <IEnumerable <B> > Traverse <A, B>(this IEnumerable <OptionUnsafe <A> > ma, Func <A, B> f) { var res = new List <B>(); foreach (var xs in ma) { if (xs.IsNone) { return(None); } res.Add(f(xs.Value)); } return(OptionUnsafe <IEnumerable <B> > .Some(Seq.FromArray <B>(res.ToArray()))); }
public static Eff <RT, Seq <B> > Traverse <RT, A, B>(this Seq <Eff <RT, A> > ma, Func <A, B> f) where RT : struct => EffMaybe <RT, Seq <B> >(env => { var rs = new List <B>(); foreach (var m in ma) { var r = m.Run(env); if (r.IsFail) { return(FinFail <Seq <B> >(r.Error)); } rs.Add(f(r.Value)); } return(FinSucc(Seq.FromArray(rs.ToArray()))); });
public static Aff <Seq <B> > TraverseSerial <A, B>(this Seq <Aff <A> > ma, Func <A, B> f) => AffMaybe <Seq <B> >(async() => { var rs = new List <B>(); foreach (var m in ma) { var r = await m.Run().ConfigureAwait(false); if (r.IsFail) { return(FinFail <Seq <B> >(r.Error)); } rs.Add(f(r.Value)); } return(FinSucc(Seq.FromArray(rs.ToArray()))); });
public static Fin <Seq <B> > Traverse <A, B>(this Seq <Fin <A> > ma, Func <A, B> f) { var res = new B[ma.Count]; var ix = 0; foreach (var xs in ma) { if (xs.IsFail) { return(xs.Cast <Seq <B> >()); } res[ix] = f(xs.Value); ix++; } return(Fin <Seq <B> > .Succ(Seq.FromArray <B>(res))); }
public static OptionUnsafe <Seq <B> > Traverse <A, B>(this Seq <OptionUnsafe <A> > ma, Func <A, B> f) { var res = new B[ma.Count]; var ix = 0; foreach (var xs in ma) { if (xs.IsNone) { return(None); } res[ix] = f(xs.Value); ix++; } return(OptionUnsafe <Seq <B> > .Some(Seq.FromArray <B>(res))); }
public static Aff <RT, Seq <B> > TraverseSerial <RT, A, B>(this Seq <Aff <RT, A> > ma, Func <A, B> f) where RT : struct, HasCancel <RT> => AffMaybe <RT, Seq <B> >(async env => { var rs = new List <B>(); foreach (var m in ma) { var r = await m.Run(env); if (r.IsFail) { return(FinFail <Seq <B> >(r.Error)); } rs.Add(f(r.Value)); } return(FinSucc(Seq.FromArray(rs.ToArray()))); });
public static EitherUnsafe <L, IEnumerable <B> > Traverse <L, A, B>(this IEnumerable <EitherUnsafe <L, A> > ma, Func <A, B> f) { var res = new List <B>(); foreach (var x in ma) { if (x.IsLeft) { return(EitherUnsafe <L, IEnumerable <B> > .Left((L)x)); } else { res.Add(f((A)x)); } } return(Seq.FromArray <B>(res.ToArray())); }
public static Try <Seq <B> > Traverse <A, B>(this Seq <Try <A> > ma, Func <A, B> f) => () => { var res = new B[ma.Count]; var ix = 0; foreach (var xs in ma) { var x = xs(); if (x.IsFaulted) { return(new Result <Seq <B> >(x.Exception)); } res[ix] = f(x.Value); ix++; } return(new Result <Seq <B> >(Seq.FromArray(res))); };
public static EitherUnsafe <L, Seq <B> > Traverse <L, A, B>(this Seq <EitherUnsafe <L, A> > ma, Func <A, B> f) { var res = new B[ma.Count]; var ix = 0; foreach (var x in ma) { if (x.IsLeft) { return(EitherUnsafe <L, Seq <B> > .Left((L)x)); } else { res[ix] = f((A)x); ix++; } } return(Seq.FromArray <B>(res)); }
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()))); }