public static EitherAsync <L, OptionAsync <B> > Traverse <L, A, B>(this OptionAsync <EitherAsync <L, A> > ma, Func <A, B> f) { return(new EitherAsync <L, OptionAsync <B> >(Go(ma, f))); async Task <EitherData <L, OptionAsync <B> > > Go(OptionAsync <EitherAsync <L, A> > ma, Func <A, B> f) { var(isSome, value) = await ma.Data.ConfigureAwait(false); if (!isSome) { return(EitherData.Right <L, OptionAsync <B> >(OptionAsync <B> .None)); } var db = await value.Data.ConfigureAwait(false); if (db.State == EitherStatus.IsBottom) { return(EitherData <L, OptionAsync <B> > .Bottom); } if (db.State == EitherStatus.IsLeft) { return(EitherData.Left <L, OptionAsync <B> >(db.Left)); } return(EitherData.Right <L, OptionAsync <B> >(OptionAsync <B> .Some(f(db.Right)))); } }
public static TryOptionAsync <OptionAsync <B> > Traverse <A, B>(this OptionAsync <TryOptionAsync <A> > ma, Func <A, B> f) { return(ToTry(() => Go(ma, f))); async Task <OptionalResult <OptionAsync <B> > > Go(OptionAsync <TryOptionAsync <A> > ma, Func <A, B> f) { var(isSome, value) = await ma.Data.ConfigureAwait(false); if (!isSome) { return(new OptionalResult <OptionAsync <B> >(OptionAsync <B> .None)); } var rb = await value.Try().ConfigureAwait(false); if (rb.IsFaulted) { return(new OptionalResult <OptionAsync <B> >(rb.Exception)); } if (rb.IsNone) { return(OptionalResult <OptionAsync <B> > .None); } return(new OptionalResult <OptionAsync <B> >(OptionAsync <B> .Some(f(rb.Value.Value)))); } }
public static async ValueTask <OptionAsync <B> > Traverse <A, B>(this OptionAsync <ValueTask <A> > ma, Func <A, B> f) { var(s, v) = await ma.Data.ConfigureAwait(false); if (!s) { return(OptionAsync <B> .None); } var a = await v.ConfigureAwait(false); return(OptionAsync <B> .Some(f(a))); }
public static async Task <OptionAsync <B> > Traverse <A, B>(this OptionAsync <Task <A> > ma, Func <A, B> f) { var(s, v) = await ma.Data; if (!s) { return(OptionAsync <B> .None); } var a = await v; if (v.IsFaulted) { ExceptionDispatchInfo.Capture(v.Exception.InnerException).Throw(); } return(OptionAsync <B> .Some(f(a))); }
public void SetResult(A result) => Task = OptionAsync <A> .Some(result);