您好我有cats
库
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)
}
})
其中verifyCardId
和verifyAddress
是一个外部API调用,返回Future
,这在某种程度上非常昂贵且耗时。
问题是如何执行以下操作:
如何改进现有代码?谢谢堆
编辑: 添加有关首选项的更多信息,以跳过一个昂贵的API调用
答案 0 :(得分:0)
flatMap
绑定monad)。CheckResult
应调整为丢失卡片的情况。扩展样本
case class Patron(name: String, cardId: String, address: String)
case class CheckResult(name: String, idCheck: Option[Boolean], addressCheck: Boolean)
def verifyCardId(cardId: String) = Future{
Thread.sleep(5000)
Some(true)
}
def verifyAddress(address: String) = Future{
Thread.sleep(5000)
Some(true)
}
val patrons = List(Patron("p_name", "1234", "Somewhere St. 42"))
val start = LocalDateTime.now()
val results = patrons.map(p => {
(verifyCardId(p.cardId), verifyAddress(p.address)).map2(
(maybeValidCard, maybeValidAddress) => {
for {
addressCheck <- maybeValidAddress
} yield CheckResult(p.name, maybeValidCard, addressCheck)
})
})
val headResult = Await.result(results.head, Duration.Inf)
val end = LocalDateTime.now()
val duration = ChronoUnit.SECONDS.between(start, end)
短输出
patrons: List[Patron] = List(Patron(p_name,1234,Somewhere St. 42))
headResult: Option[CheckResult] = Some(CheckResult(p_name,Some(true),true))
duration: Long = 5