Hi I have the following Scala code with cats
library
results = patrons.map(p => {
(verifyCardId(p.cardId), verifyAddress(p.address)).map2(
(maybeValidCard, maybeValidAddress) => {
val result = for {
idCheck <- maybeValidCard
addressCheck <- maybeValidAddress
} yield CheckResult(p.name, idCheck, addressCheck)
}
})
where verifyCardId
and verifyAddress
is an external API call returning a Future
which are somehow very expensive and time consuming.
The question is how do I do the following:
- If one of the patron does not have a card, the code should be able to skip checking the card must still check patron's address
- If the patron has both then, the code should check the card and the address
How can I improve the existing code? Thanks heaps
Edit: Add more information about the preference to skip one of the expensive API calls