Skip to content

Instantly share code, notes, and snippets.

@NoelKennedy
Created April 6, 2012 22:47
Show Gist options
  • Save NoelKennedy/2323679 to your computer and use it in GitHub Desktop.
Save NoelKennedy/2323679 to your computer and use it in GitHub Desktop.
Either<T,U> in action
public abstract partial class Either<T, U> {...}
//here are some business rules
public Either<LoanDeclinedReason, LoanRequest> CheckCustomerCredit(LoanRequest request) {
if (request.Customer.CreditScore > 8) return Either.Right(request);
return Either.Left(LoanDeclinedReason.InsufficientCreditScore);
}
public Either<LoanDeclinedReason, LoanRequest> CheckGoldPrerequisiteCustomer(LoanRequest request) {
if (request.Value < 10000) return Either.Right(request); //dont need gold status for less than 10,000
if (request.Customer.GoldCustomer) return Either.Right(request);
return Either.Left(LoanDeclinedReason.RequiresGoldCustomer);
}
public Either<LoanDeclinedReason, LoanRequest> CheckSillyAmount(LoanRequest request) {
if (request.Value > 1000000)
return Either.Left(LoanDeclinedReason.LoanRequestTooHigh);
return Either.Right(request);
}
public void LoanApproval(){
LoanRequest request = new LoanRequest {
Customer = new Customer { CreditScore = 10, GoldCustomer = true },
Value = 9000};
Either<LoanDeclinedReason, LoanRequest> checkOne = CheckCustomerCredit(request);
Either<LoanDeclinedReason, LoanRequest> checkTwo = checkOne.FlatMap(CheckGoldPrerequisiteCustomer);
Either<LoanDeclinedReason, LoanRequest> checkThree = checkTwo.FlatMap(CheckSillyAmount);
bool loanApprovalStatus = checkThree.Fold(failReason=>false,passedLoan=>true);
loanApprovalStatus.Should().BeTrue();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment