Scala:SeqT monad 转换器?
Scala: SeqT monad transformer?
如果我们有这样两个函数...
def findUserById(id: Long): Future[Option[User]] = ???
def findAddressByUser(user: User): Future[Option[Address]] = ???
...然后我们就可以使用 cats OptionT
monad transformer 轻松地编写用于理解的内容:
for {
user <- OptionT(findUserById(id))
address <- OptionT(findAddressByUser(user))
} ...
我想用这种方式组合未来的序列,像这样:
def findUsersBySomeField(value: FieldValue): Future[Seq[User]] = ???
def findAddressesByUser(user: User): Future[Seq[Address]] = ???
for {
user <- SeqT(findUsersBySomeField(value))
address <- SeqT(findAddressesByUser(user))
} ...
但我在 Cats 或 Scalaz 中找不到任何 SeqT
实现。是否存在此类 monad 转换器的某些实现,或者我需要自己编写 monad 转换器?不是太难,只是不想重新发明轮子
(我问题开头的例子来自this article)
猫,从 1.0.0-MF 开始就没有了。在 their FAQ:
中有解释
A naive implementation of ListT
suffers from associativity issues; see this gist for an example. It's possible to create a ListT
that doesn't have these issues, but it tends to be pretty inefficient. For many use-cases, Nested can be used to achieve the desired results.
Scalaz,截至 7.2.15 有 StreamT
和 ListT
,尽管后者 has 关联性问题。
如果我们有这样两个函数...
def findUserById(id: Long): Future[Option[User]] = ???
def findAddressByUser(user: User): Future[Option[Address]] = ???
...然后我们就可以使用 cats OptionT
monad transformer 轻松地编写用于理解的内容:
for {
user <- OptionT(findUserById(id))
address <- OptionT(findAddressByUser(user))
} ...
我想用这种方式组合未来的序列,像这样:
def findUsersBySomeField(value: FieldValue): Future[Seq[User]] = ???
def findAddressesByUser(user: User): Future[Seq[Address]] = ???
for {
user <- SeqT(findUsersBySomeField(value))
address <- SeqT(findAddressesByUser(user))
} ...
但我在 Cats 或 Scalaz 中找不到任何 SeqT
实现。是否存在此类 monad 转换器的某些实现,或者我需要自己编写 monad 转换器?不是太难,只是不想重新发明轮子
(我问题开头的例子来自this article)
猫,从 1.0.0-MF 开始就没有了。在 their FAQ:
中有解释A naive implementation of
ListT
suffers from associativity issues; see this gist for an example. It's possible to create aListT
that doesn't have these issues, but it tends to be pretty inefficient. For many use-cases, Nested can be used to achieve the desired results.
Scalaz,截至 7.2.15 有 StreamT
和 ListT
,尽管后者 has 关联性问题。