public static Try <IEnumerable <B> > Traverse <A, B>( this IEnumerable <A> enumerable, Func <A, Try <B> > callback) { IEnumerable <Try <B> > EnumerateCallback() { foreach (var enume in enumerable) { var execution = callback(enume); yield return(execution); if (execution.IsError) { yield break; } } } var mapped = EnumerateCallback().ToList(); var maybeError = mapped.FirstOrNone(x => x.IsError).Select(x => x.AsError().Value); return(!maybeError.IsSome ? Try <IEnumerable <B> > .Create(mapped.Select(x => x.AsSuccess().Value)) : maybeError.ForceValue()); }
public static IEnumerable <Try <B> > Traverse <A, B>( this Try <A> either, Func <A, IEnumerable <B> > callback ) { var tryEnumerable = either.Select(callback); return(tryEnumerable.IsSuccess ? tryEnumerable.AsSuccess().Value.Select(x => x.AsTry()) : new[] { Try <B> .Create(tryEnumerable.AsError().Value) }); }
protected override Try <Next> MapImpl <Next>(Func <Success, Next> f) { try { var result = f(Value); return(Try <Next> .Create(result)); } catch (Exception e) { return(Try <Next> .Create(e)); } }
public static Try <A> AsTry <A, Error>(this Either <A, Error> either, Func <Error, Exception> errorTransform) => either.Unify(Try <A> .Create, e => Try <A> .Create(errorTransform(e)));
public static Try <A> AsTry <A>(this Try <Option <A> > option, Func <Exception> errorCallback) => option.SelectMany(o => o.Select(Try <A> .Create).Else(() => Try <A> .Create(errorCallback())));
public static Try <A> AsTry <A>(this A thing) => Try <A> .Create(thing);