[英]Scala for comprehension with Future, List and Option
I am building a reactive site in Scala and Play Framework, and my data models are such that I often need to compose Future
and Option
, and build Future
of List
/ Set
from previous values to get the result I need. 我在斯卡拉建设一个活性位点和播放框架,以及我的数据模型是这样的,我经常需要撰写
Future
和Option
,并建立Future
的List
/ Set
从以前的值以得到结果,我所需要的。
I wrote a simple app with a fake data source that you can copy and paste and it should compile. 我写了一个带有假数据源的简单应用程序,你可以复制和粘贴它,它应该编译。 My question is, how can I get the result back, in my case
UserContext
, in a consumable form. 我的问题是,如何以可消费的形式将结果返回到我的案例
UserContext
中。 Currently, I am getting back Future[Option[Future[UserContext]]]
. 目前,我要回到
Future[Option[Future[UserContext]]]
。
I want to do this in pure Scala to learn the language better, so I am avoiding Scalaz at the moment. 我想在纯Scala中这样做以更好地学习语言,所以我现在正在避开Scalaz。 Although I know I should eventually use that.
虽然我知道我最终应该使用它。
package futures
import scala.concurrent.{Future, ExecutionContext}
// http://www.edofic.com/posts/2014-03-07-practical-future-option.html
case class FutureO[+A](future: Future[Option[A]]) extends AnyVal {
def flatMap[B](f: A => FutureO[B])(implicit ec: ExecutionContext): FutureO[B] = {
FutureO {
future.flatMap { optA =>
optA.map { a =>
f(a).future
} getOrElse Future.successful(None)
}
}
}
def map[B](f: A => B)(implicit ec: ExecutionContext): FutureO[B] = {
FutureO(future.map(_ map f))
}
}
// ========== USAGE OF FutureO BELOW ============= \\
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
object TeamDB {
val basketballTeam = Team(id = 111, player_ids = Set(111, 222))
val baseballTeam = Team(id = 222, player_ids = Set(333))
def findById(teamId: Int): Future[Option[Team]] = Future.successful(
teamId match {
case 111 => Some(basketballTeam)
case 222 => Some(baseballTeam)
case _ => None
}
)
}
object PlayerDB {
val basketballPlayer1 = Player(id = 111, jerseyNumber = 23)
val basketballPlayer2 = Player(id = 222, jerseyNumber = 45)
val baseballPlayer = Player(id = 333, jerseyNumber = 5)
def findById(playerId: Int): Future[Option[Player]] = Future.successful(
playerId match {
case 111 => Some(basketballPlayer1)
case 222 => Some(basketballPlayer2)
case 333 => Some(baseballPlayer)
case _ => None
}
)
}
object UserDB {
// user1 is on BOTH the baseball and basketball team
val user1 = User(id = 111, name = "Michael Jordan", player_ids = Set(111, 333), team_ids = Set(111, 222))
// user2 is ONLY on the basketball team
val user2 = User(id = 222, name = "David Wright", player_ids = Set(222), team_ids = Set(111))
def findById(userId: Long): Future[Option[User]] = Future.successful(
userId match {
case 111 => Some(user1)
case 222 => Some(user2)
case _ => None
}
)
}
case class User(id: Int, name: String, player_ids: Set[Int], team_ids: Set[Int])
case class Player(id: Int, jerseyNumber: Int)
case class Team(id: Int, player_ids: Set[Int])
case class UserContext(user: User, teams: Set[Team], players: Set[Player])
object FutureOptionListTest extends App {
val result = for {
user <- FutureO(UserDB.findById(userId = 111))
} yield for {
players: Set[Option[Player]] <- Future.traverse(user.player_ids)(x => PlayerDB.findById(x))
teams: Set[Option[Team]] <- Future.traverse(user.team_ids)(x => TeamDB.findById(x))
} yield {
UserContext(user, teams.flatten, players.flatten)
}
result.future // returns Future[Option[Future[UserContext]]] but I just want Future[UserContext] or UserContext
}
You have created FutureO
which combines the effects of Future
and Option
(if you are looking into Scalaz this compares with OptionT[Future, ?]
). 您创建了
FutureO
,它结合了Future
和Option
的效果(如果您正在研究Scalaz, OptionT[Future, ?]
与OptionT[Future, ?]
)。
Remembering that for ... yield
is analogous to FutureO.map
, the result type will always be FutureO[?]
(and Future[Option[?]]
if you do result.future
). 记住,
for ... yield
类似于FutureO.map
,结果类型将始终是FutureO[?]
(和Future[Option[?]]
如果你执行result.future
)。
The problem is you want to return a Future[UserContex]
instead of a Future[Option[UserContext]]
. 问题是你想要返回
Future[UserContex]
而不是Future[Option[UserContext]]
。 Essentially you want to loose the Option
context, so somewhere you need to explicitly handle if the user exists or not. 基本上你想要松开
Option
上下文,所以你需要明确处理用户是否存在的地方。
A possible solution in this case could be to leave out the FutureO
since you are only using it once. 在这种情况下可能的解决方案可能是遗漏
FutureO
因为您只使用它一次。
case class NoUserFoundException(id: Long) extends Exception
// for comprehension with Future
val result = for {
user <- UserDB.findById(userId = 111) flatMap (
// handle Option (Future[Option[User]] => Future[User])
_.map(user => Future.successful(user))
.getOrElse(Future.failed(NoUserFoundException(111)))
)
players <- Future.traverse(user.player_ids)(x => PlayerDB.findById(x))
teams <- Future.traverse(user.team_ids)(x => TeamDB.findById(x))
} yield UserContext(user, teams.flatten, players.flatten)
// result: scala.concurrent.Future[UserContext]
If you had multiple functions returning a Future[Option[?]]
, you probably would like to use FutureO
, in this case you could create an extra function Future[A] => FutureO[A]
, so you can use your functions in the same for
comprehension (all in the FutureO
monad): 如果你有多个函数返回
Future[Option[?]]
,你可能想使用FutureO
,在这种情况下你可以创建一个额外的函数Future[A] => FutureO[A]
,这样你就可以使用你的函数了同样for
理解(所有在FutureO
monad):
def liftFO[A](fut: Future[A]) = FutureO(fut.map(Some(_)))
// for comprehension with FutureO
val futureO = for {
user <- FutureO(UserDB.findById(userId = 111))
players <- liftFO(Future.traverse(user.player_ids)(x => PlayerDB.findById(x)))
teams <- liftFO(Future.traverse(user.team_ids)(x => TeamDB.findById(x)))
} yield UserContext(user, teams.flatten, players.flatten)
// futureO: FutureO[UserContext]
val result = futureO.future flatMap (
// handle Option (Future[Option[UserContext]] => Future[UserContext])
_.map(user => Future.successful(user))
.getOrElse(Future.failed(new RuntimeException("Could not find UserContext")))
)
// result: scala.concurrent.Future[UserContext]
But as you can see, you will always need to handle the "option context" before you can return a Future[UserContext]
. 但正如您所看到的,在返回
Future[UserContext]
之前,您始终需要处理“选项上下文”。
To expand on Peter Neyens' answer, often I'll put a bunch of monad -> monad transformations in a special implicit class and import them as I need them. 为了扩展Peter Neyens的答案,我经常会在一个特殊的隐式类中放入一堆monad - > monad转换,并根据需要导入它们。 Here we have two monads,
Option[T]
and Future[T]
. 这里我们有两个monad,
Option[T]
和Future[T]
。 In this case, you are treating None
as being a failed Future
. 在这种情况下,您将
None
视为失败的Future
。 You could probably do this: 你可能会这样做:
package foo {
class OptionOps[T](in: Option[T]) {
def toFuture: Future[T] = in match {
case Some(t) => Future.successful(t)
case None => Future.failed(new Exception("option was none"))
}
}
implicit def optionOps[T](in: Option[T]) = new OptionOps[T](in)
}
Then you just import it import foo.optionOps
然后你只需导入它
import foo.optionOps
And then: 接着:
val a: Future[Any] = ...
val b: Option[Any] = Some("hi")
for {
aFuture <- a
bFuture <- b.toFuture
} yield bFuture // yields a successful future containing "hi"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.