Skip to content

Commit

Permalink
refactor: remove all redundant implementations of method overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
daht-x authored Jan 5, 2024
1 parent 7c25328 commit 53b864a
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 172 deletions.
53 changes: 17 additions & 36 deletions source/Monads/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,86 +48,67 @@ public static implicit operator Result<TSuccess, TFailure>(TFailure failure)

/// <summary>Creates a new failed result if the value of <paramref name="predicate" /> is <see langword="true" />; otherwise, returns the previous result.</summary>
/// <param name="predicate">Creates a set of criteria.</param>
/// <param name="failure">The possible failure.</param>
/// <param name="createFailure">Creates the possible failure.</param>
/// <returns>A new failed result if the value of <paramref name="predicate" /> is <see langword="true" />; otherwise, the previous result.</returns>
public Result<TSuccess, TFailure> Ensure(Func<TSuccess, bool> predicate, TFailure failure)
{
if (IsFailed)
{
return this;
}
return predicate(Success)
? new(failure)
: this;
}
public Result<TSuccess, TFailure> Ensure(Func<TSuccess, bool> predicate, Func<TSuccess, TFailure> createFailure)
=> Ensure(predicate, createFailure(Success));

/// <summary>Creates a new failed result if the value of <paramref name="predicate" /> is <see langword="true" />; otherwise, returns the previous result.</summary>
/// <param name="predicate">Creates a set of criteria.</param>
/// <param name="createFailure">Creates the possible failure.</param>
/// <param name="failure">The possible failure.</param>
/// <returns>A new failed result if the value of <paramref name="predicate" /> is <see langword="true" />; otherwise, the previous result.</returns>
public Result<TSuccess, TFailure> Ensure(Func<TSuccess, bool> predicate, Func<TSuccess, TFailure> createFailure)
public Result<TSuccess, TFailure> Ensure(Func<TSuccess, bool> predicate, TFailure failure)
{
if (IsFailed)
{
return this;
}
return predicate(Success)
? new(createFailure(Success))
? new(failure)
: this;
}

/// <summary>Creates a new failed result if the value of <paramref name="predicate" /> is <see langword="true" />; otherwise, returns the previous result.</summary>
/// <param name="auxiliary">The auxiliary to use in combination with <paramref name="predicate" /> and <paramref name="createFailure" />.</param>
/// <param name="createAuxiliary">Creates the auxiliary to use in combination with <paramref name="predicate" /> and <paramref name="createFailure" />.</param>
/// <param name="predicate">Creates a set of criteria.</param>
/// <param name="createFailure">Creates the possible failure.</param>
/// <typeparam name="TAuxiliary">Type of auxiliary.</typeparam>
/// <returns>A new failed result if the value of <paramref name="predicate" /> is <see langword="true" />; otherwise, the previous result.</returns>
public Result<TSuccess, TFailure> Ensure<TAuxiliary>(TAuxiliary auxiliary, Func<TSuccess, TAuxiliary, bool> predicate, Func<TSuccess, TAuxiliary, TFailure> createFailure)
{
if (IsFailed)
{
return this;
}
return predicate(Success, auxiliary)
? new(createFailure(Success, auxiliary))
: this;
}
public Result<TSuccess, TFailure> Ensure<TAuxiliary>(Func<TAuxiliary> createAuxiliary, Func<TSuccess, TAuxiliary, bool> predicate, Func<TSuccess, TAuxiliary, TFailure> createFailure)
=> Ensure(createAuxiliary(), predicate, createFailure);

/// <summary>Creates a new failed result if the value of <paramref name="predicate" /> is <see langword="true" />; otherwise, returns the previous result.</summary>
/// <param name="createAuxiliary">Creates the auxiliary to use in combination with <paramref name="predicate" /> and <paramref name="createFailure" />.</param>
/// <param name="auxiliary">The auxiliary to use in combination with <paramref name="predicate" /> and <paramref name="createFailure" />.</param>
/// <param name="predicate">Creates a set of criteria.</param>
/// <param name="createFailure">Creates the possible failure.</param>
/// <typeparam name="TAuxiliary">Type of auxiliary.</typeparam>
/// <returns>A new failed result if the value of <paramref name="predicate" /> is <see langword="true" />; otherwise, the previous result.</returns>
public Result<TSuccess, TFailure> Ensure<TAuxiliary>(Func<TAuxiliary> createAuxiliary, Func<TSuccess, TAuxiliary, bool> predicate, Func<TSuccess, TAuxiliary, TFailure> createFailure)
public Result<TSuccess, TFailure> Ensure<TAuxiliary>(TAuxiliary auxiliary, Func<TSuccess, TAuxiliary, bool> predicate, Func<TSuccess, TAuxiliary, TFailure> createFailure)
{
if (IsFailed)
{
return this;
}
TAuxiliary auxiliary = createAuxiliary();
return predicate(Success, auxiliary)
? new(createFailure(Success, auxiliary))
: this;
}

/// <summary>Creates a new result with the same or different type of expected success.</summary>
/// <param name="successToMap">The expected success to map.</param>
/// <param name="createSuccessToMap">Creates the expected success to map.</param>
/// <typeparam name="TSuccessToMap">Type of expected success to map.</typeparam>
/// <returns>A new result with the same or different type of expected success.</returns>
public Result<TSuccessToMap, TFailure> Map<TSuccessToMap>(TSuccessToMap successToMap)
=> IsFailed
? new(Failure)
: new(successToMap);
public Result<TSuccessToMap, TFailure> Map<TSuccessToMap>(Func<TSuccess, TSuccessToMap> createSuccessToMap)
=> Map(createSuccessToMap(Success));

/// <summary>Creates a new result with the same or different type of expected success.</summary>
/// <param name="createSuccessToMap">Creates the expected success to map.</param>
/// <param name="successToMap">The expected success to map.</param>
/// <typeparam name="TSuccessToMap">Type of expected success to map.</typeparam>
/// <returns>A new result with the same or different type of expected success.</returns>
public Result<TSuccessToMap, TFailure> Map<TSuccessToMap>(Func<TSuccess, TSuccessToMap> createSuccessToMap)
public Result<TSuccessToMap, TFailure> Map<TSuccessToMap>(TSuccessToMap successToMap)
=> IsFailed
? new(Failure)
: new(createSuccessToMap(Success));
: new(successToMap);

/// <summary>Creates a new result in combination with another result with the same or different type of expected success.</summary>
/// <param name="createResultToBind">Creates a new result to bind.</param>
Expand Down
74 changes: 23 additions & 51 deletions source/Monads/ResultFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,22 @@ public static Result<TSuccess, TFailure> Catch<TException, TSuccess, TFailure>(F
/// <summary>Creates a new failed result if the value of <paramref name="predicate" /> is <see langword="true" />; otherwise, returns the previous result.</summary>
/// <param name="success">The expected success.</param>
/// <param name="predicate">Creates a set of criteria.</param>
/// <param name="failure">The possible failure.</param>
/// <param name="createFailure">Creates the possible failure.</param>
/// <typeparam name="TSuccess">Type of expected success.</typeparam>
/// <typeparam name="TFailure">Type of possible failure.</typeparam>
/// <returns>A new failed result if the value of <paramref name="predicate" /> is <see langword="true" />; otherwise, the previous result.</returns>
public static Result<TSuccess, TFailure> Ensure<TSuccess, TFailure>(TSuccess success, Func<TSuccess, bool> predicate, TFailure failure)
=> predicate(success)
? Fail<TSuccess, TFailure>(failure)
: Succeed<TSuccess, TFailure>(success);
public static Result<TSuccess, TFailure> Ensure<TSuccess, TFailure>(TSuccess success, Func<TSuccess, bool> predicate, Func<TSuccess, TFailure> createFailure)
=> Ensure(success, predicate, createFailure(success));

/// <summary>Creates a new failed result if the value of <paramref name="predicate" /> is <see langword="true" />; otherwise, returns the previous result.</summary>
/// <param name="success">The expected success.</param>
/// <param name="createSuccess">Creates the expected success.</param>
/// <param name="predicate">Creates a set of criteria.</param>
/// <param name="createFailure">Creates the possible failure.</param>
/// <typeparam name="TSuccess">Type of expected success.</typeparam>
/// <typeparam name="TFailure">Type of possible failure.</typeparam>
/// <returns>A new failed result if the value of <paramref name="predicate" /> is <see langword="true" />; otherwise, the previous result.</returns>
public static Result<TSuccess, TFailure> Ensure<TSuccess, TFailure>(TSuccess success, Func<TSuccess, bool> predicate, Func<TSuccess, TFailure> createFailure)
=> predicate(success)
? Fail<TSuccess, TFailure>(createFailure(success))
: Succeed<TSuccess, TFailure>(success);
public static Result<TSuccess, TFailure> Ensure<TSuccess, TFailure>(Func<TSuccess> createSuccess, Func<TSuccess, bool> predicate, Func<TSuccess, TFailure> createFailure)
=> Ensure(createSuccess(), predicate, createFailure);

/// <summary>Creates a new failed result if the value of <paramref name="predicate" /> is <see langword="true" />; otherwise, returns the previous result.</summary>
/// <param name="createSuccess">Creates the expected success.</param>
Expand All @@ -56,58 +52,43 @@ public static Result<TSuccess, TFailure> Ensure<TSuccess, TFailure>(TSuccess suc
/// <typeparam name="TFailure">Type of possible failure.</typeparam>
/// <returns>A new failed result if the value of <paramref name="predicate" /> is <see langword="true" />; otherwise, the previous result.</returns>
public static Result<TSuccess, TFailure> Ensure<TSuccess, TFailure>(Func<TSuccess> createSuccess, Func<TSuccess, bool> predicate, TFailure failure)
{
TSuccess success = createSuccess();
return predicate(success)
? Fail<TSuccess, TFailure>(failure)
: Succeed<TSuccess, TFailure>(success);
}
=> Ensure(createSuccess(), predicate, failure);

/// <summary>Creates a new failed result if the value of <paramref name="predicate" /> is <see langword="true" />; otherwise, returns the previous result.</summary>
/// <param name="createSuccess">Creates the expected success.</param>
/// <param name="success">The expected success.</param>
/// <param name="predicate">Creates a set of criteria.</param>
/// <param name="createFailure">Creates the possible failure.</param>
/// <param name="failure">The possible failure.</param>
/// <typeparam name="TSuccess">Type of expected success.</typeparam>
/// <typeparam name="TFailure">Type of possible failure.</typeparam>
/// <returns>A new failed result if the value of <paramref name="predicate" /> is <see langword="true" />; otherwise, the previous result.</returns>
public static Result<TSuccess, TFailure> Ensure<TSuccess, TFailure>(Func<TSuccess> createSuccess, Func<TSuccess, bool> predicate, Func<TSuccess, TFailure> createFailure)
{
TSuccess success = createSuccess();
return predicate(success)
? Fail<TSuccess, TFailure>(createFailure(success))
public static Result<TSuccess, TFailure> Ensure<TSuccess, TFailure>(TSuccess success, Func<TSuccess, bool> predicate, TFailure failure)
=> predicate(success)
? Fail<TSuccess, TFailure>(failure)
: Succeed<TSuccess, TFailure>(success);
}

/// <summary>Creates a new failed result if the value of <paramref name="predicate" /> is <see langword="true" />; otherwise, returns the previous result.</summary>
/// <param name="success">The expected success.</param>
/// <param name="auxiliary">The auxiliary to use in combination with <paramref name="predicate" /> and <paramref name="createFailure" />.</param>
/// <param name="createAuxiliary">Creates the auxiliary to use in combination with <paramref name="predicate" /> and <paramref name="createFailure" />.</param>
/// <param name="predicate">Creates a set of criteria.</param>
/// <param name="createFailure">Creates the possible failure.</param>
/// <typeparam name="TAuxiliary">Type of auxiliary.</typeparam>
/// <typeparam name="TSuccess">Type of expected success.</typeparam>
/// <typeparam name="TFailure">Type of possible failure.</typeparam>
/// <returns>A new failed result if the value of <paramref name="predicate" /> is <see langword="true" />; otherwise, the previous result.</returns>
public static Result<TSuccess, TFailure> Ensure<TAuxiliary, TSuccess, TFailure>(TSuccess success, TAuxiliary auxiliary, Func<TSuccess, TAuxiliary, bool> predicate, Func<TSuccess, TAuxiliary, TFailure> createFailure)
=> predicate(success, auxiliary)
? Fail<TSuccess, TFailure>(createFailure(success, auxiliary))
: Succeed<TSuccess, TFailure>(success);
public static Result<TSuccess, TFailure> Ensure<TAuxiliary, TSuccess, TFailure>(TSuccess success, Func<TAuxiliary> createAuxiliary, Func<TSuccess, TAuxiliary, bool> predicate, Func<TSuccess, TAuxiliary, TFailure> createFailure)
=> Ensure(success, createAuxiliary(), predicate, createFailure);

/// <summary>Creates a new failed result if the value of <paramref name="predicate" /> is <see langword="true" />; otherwise, returns the previous result.</summary>
/// <param name="success">The expected success.</param>
/// <param name="createSuccess">Creates the expected success.</param>
/// <param name="createAuxiliary">Creates the auxiliary to use in combination with <paramref name="predicate" /> and <paramref name="createFailure" />.</param>
/// <param name="predicate">Creates a set of criteria.</param>
/// <param name="createFailure">Creates the possible failure.</param>
/// <typeparam name="TAuxiliary">Type of auxiliary.</typeparam>
/// <typeparam name="TSuccess">Type of expected success.</typeparam>
/// <typeparam name="TFailure">Type of possible failure.</typeparam>
/// <returns>A new failed result if the value of <paramref name="predicate" /> is <see langword="true" />; otherwise, the previous result.</returns>
public static Result<TSuccess, TFailure> Ensure<TAuxiliary, TSuccess, TFailure>(TSuccess success, Func<TAuxiliary> createAuxiliary, Func<TSuccess, TAuxiliary, bool> predicate, Func<TSuccess, TAuxiliary, TFailure> createFailure)
{
TAuxiliary auxiliary = createAuxiliary();
return predicate(success, auxiliary)
? Fail<TSuccess, TFailure>(createFailure(success, auxiliary))
: Succeed<TSuccess, TFailure>(success);
}
public static Result<TSuccess, TFailure> Ensure<TAuxiliary, TSuccess, TFailure>(Func<TSuccess> createSuccess, Func<TAuxiliary> createAuxiliary, Func<TSuccess, TAuxiliary, bool> predicate, Func<TSuccess, TAuxiliary, TFailure> createFailure)
=> Ensure(createSuccess(), createAuxiliary, predicate, createFailure);

/// <summary>Creates a new failed result if the value of <paramref name="predicate" /> is <see langword="true" />; otherwise, returns the previous result.</summary>
/// <param name="createSuccess">Creates the expected success.</param>
Expand All @@ -119,30 +100,21 @@ public static Result<TSuccess, TFailure> Ensure<TAuxiliary, TSuccess, TFailure>(
/// <typeparam name="TFailure">Type of possible failure.</typeparam>
/// <returns>A new failed result if the value of <paramref name="predicate" /> is <see langword="true" />; otherwise, the previous result.</returns>
public static Result<TSuccess, TFailure> Ensure<TAuxiliary, TSuccess, TFailure>(Func<TSuccess> createSuccess, TAuxiliary auxiliary, Func<TSuccess, TAuxiliary, bool> predicate, Func<TSuccess, TAuxiliary, TFailure> createFailure)
{
TSuccess success = createSuccess();
return predicate(success, auxiliary)
? Fail<TSuccess, TFailure>(createFailure(success, auxiliary))
: Succeed<TSuccess, TFailure>(success);
}
=> Ensure(createSuccess(), auxiliary, predicate, createFailure);

/// <summary>Creates a new failed result if the value of <paramref name="predicate" /> is <see langword="true" />; otherwise, returns the previous result.</summary>
/// <param name="createSuccess">Creates the expected success.</param>
/// <param name="createAuxiliary">Creates the auxiliary to use in combination with <paramref name="predicate" /> and <paramref name="createFailure" />.</param>
/// <param name="success">The expected success.</param>
/// <param name="auxiliary">The auxiliary to use in combination with <paramref name="predicate" /> and <paramref name="createFailure" />.</param>
/// <param name="predicate">Creates a set of criteria.</param>
/// <param name="createFailure">Creates the possible failure.</param>
/// <typeparam name="TAuxiliary">Type of auxiliary.</typeparam>
/// <typeparam name="TSuccess">Type of expected success.</typeparam>
/// <typeparam name="TFailure">Type of possible failure.</typeparam>
/// <returns>A new failed result if the value of <paramref name="predicate" /> is <see langword="true" />; otherwise, the previous result.</returns>
public static Result<TSuccess, TFailure> Ensure<TAuxiliary, TSuccess, TFailure>(Func<TSuccess> createSuccess, Func<TAuxiliary> createAuxiliary, Func<TSuccess, TAuxiliary, bool> predicate, Func<TSuccess, TAuxiliary, TFailure> createFailure)
{
TSuccess success = createSuccess();
TAuxiliary auxiliary = createAuxiliary();
return predicate(success, auxiliary)
public static Result<TSuccess, TFailure> Ensure<TAuxiliary, TSuccess, TFailure>(TSuccess success, TAuxiliary auxiliary, Func<TSuccess, TAuxiliary, bool> predicate, Func<TSuccess, TAuxiliary, TFailure> createFailure)
=> predicate(success, auxiliary)
? Fail<TSuccess, TFailure>(createFailure(success, auxiliary))
: Succeed<TSuccess, TFailure>(success);
}

/// <summary>Creates a new successful result.</summary>
/// <param name="success">The expected success.</param>
Expand Down
Loading

0 comments on commit 53b864a

Please sign in to comment.