Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simplify via readable implicit
// Before
def buildItem(eob:EOB)(props: Props): Result[EOB_item] = {
implicit val empty: String = "" // weird
for (sDate <- props valueOf DateOfServicePerItem;
date <- DateFormat("MM/dd/yyyy").parseCurrent(sDate);
description <- props valueOf ProcedureDescription;
sBilled <- props valueOf BilledPerItem;
billed <- dollars(sBilled);
sInsPaid <- props valueOf PaidByPlanPerItem;
insPaid <- dollars(sInsPaid);
sDeductible <- props valueOf Deductible;
deductible <- dollars(sDeductible);
sCoinsurance <- props valueOf Coinsurance;
deductible <- dollars(sDeductible);
sCopay <- props valueOf CopayPerItem;
copay <- dollars(sDeductible);
sAllowed <- props valueOf Allowed;
allowed <- dollars(sDeductible);
x <- Good(true)) yield new EOB_item() {//...
// After
val ex = new DataExtractor(props, DateFormat("MM/dd/yyyy").parseCurrent)
implicit def extractor(name: String) = ex(name)
for (date <- DateOfServicePerItem asRecentDate;
description <- ProcedureDescription requireText;
billed <- BilledPerItem $$$;
insPaid <- PaidByPlanPerItem $$$;
deductible <- Deductible $$$;
coinsurance <- Coinsurance $$$;
copay <- CopayPerItem $$$;
allowed <- Allowed $$$;
x <- Good(true)) yield new EOB_item() { // ...
// Where
class DataExtractor(props:Props, string2date: String=>Result[Date]) {
def apply(name:String) = new {
def requireText: Result[String] = props valueOf name
def text:String = requireText getOrElse ""
def $$$: Result[BigDecimalField] = requireText flatMap dollars
def asRecentDate = requireText flatMap string2date
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment