scala嵌套了对期货的理解

时间:2015-04-27 20:19:36

标签: scala for-loop future

我的案例域类如下

case class Account(randomId: String, accounts: List[String]) // for each of accounts i need to get AccountProfiles.

case class AccountProfiles(actId: String, profiles: List[String], additionalInfo: Map[String, String], ......)

case class AccountInfo(id: String, profiles:List[String]) // for each of AccountProfiles I need to construct AccountInfo

我的访问层实现签名以提取上面的域类,如下所示

getLinked(): Future[Account]
getAccountProfile(actId: String): Future[AccountProfiles]

forFuture方法的帮助下,我可以AccountInfo理解构建getLinked getAccountProfile域对象列表吗?

1 个答案:

答案 0 :(得分:0)

是的,你可以。我认为这是您正在寻找的假设AccountProfiles.actId和AccountInfo.id应该是等效的。

for {
  account <- getLinked()
  profiles <- Future.sequence(account.accounts map { id => getAccountProfile(id) })
} yield profiles map { p => AccountInfo(p.actId, p.profiles) }