private static ITry <ShoppingListWithItems, AddFoodstuffsError> Process(ApiResult <AddFoodstuffsToShoppingListResponse> response) => response.Match( s => Try.Success <ShoppingListWithItems, AddFoodstuffsError>(ToShoppingList(s)), e => Try.Error <ShoppingListWithItems, AddFoodstuffsError>(e.Message.Match( "Item already added.", _ => AddFoodstuffsError.FoodstuffAlreadyAdded )), noConn => throw new NotImplementedException()
/// <summary> /// Maps the successful result to a new try. /// </summary> public static ITry <B, E> FlatMap <A, E, B>(this ITry <A, E> t, Func <A, ITry <B, E> > f) { return(t.Match( s => f(s), e => Try.Error <B, E>(e) )); }
/// <summary> /// Turns the option into a try using the exception in case of empty option. /// </summary> public static ITry <A, E> ToTry <A, E>(this IOption <A> option, Func <Unit, E> e) { return(option.Match( val => Try.Success <A, E>(val), _ => Try.Error <A, E>(e(Unit.Value)) )); }
/// <summary> /// If the successful result passes the predicate, returns the original try. Otherwise returns erroneous try with the specified result. /// </summary> public static ITry <A, IEnumerable <E> > Where <A, E>(this ITry <A, IEnumerable <E> > t, Func <A, bool> predicate, Func <Unit, E> otherwise) { return(t.FlatMap(a => predicate(a).Match( _ => t, _ => Try.Error <A, IEnumerable <E> >(new[] { otherwise(Unit.Value) }) ))); }
/// <summary> /// If the successful result passes the predicate, returns the original try. Otherwise returns erroneous try with the specified result. /// </summary> public static ITry <A, E> Where <A, E>(this ITry <A, E> t, Func <A, bool> predicate, Func <Unit, E> otherwise) { return(t.FlatMap(a => predicate(a).Match( _ => t, _ => Try.Error <A, E>(otherwise(Unit.Value)) ))); }
private static ITry <Unit, SignUpError[]> Error(IEnumerable <SignUpError> errors) => Try.Error <Unit, SignUpError[]>(errors.ToArray());
private static ITry <IAccount, SignInError> Error(SignInError e) => Try.Error <IAccount, SignInError>(e);
public static ITry <T, E> ToTry <T, E>(this bool b, Func <Unit, T> ifTrue, Func <Unit, E> ifFalse) { return(b ? Try.Success <T, E>(ifTrue(Unit.Value)) : Try.Error <T, E>(ifFalse(Unit.Value))); }