Created
March 1, 2021 22:02
-
-
Save vpatryshev/dfdee1b6b0813c8bed5b0917df732e8c to your computer and use it in GitHub Desktop.
Simplify via readable implicit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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