kos*_*tja 9 scala either applicative scala-cats
我有一个实例,Either[Future[A], Future[B]]我想将其转换为Future[Either[A, B]].
自从我上一个问题以来,已经发布了cat 0.8.1,改变了库的结构并Xor倾向于支持Either,这在2.12中是正确的.
因此,先前接受的答案中描述的方法不再起作用.我试图找到合适的进口但失败了.
cats.instances.either._
cats.implicits._
cats.syntax.bitraverse._
Run Code Online (Sandbox Code Playgroud)
看起来似乎有道理,但不幸的是不起作用.编译仍然失败value bisequence is not a member of Either[scala.concurrent.Future[A],scala.concurrent.Future[??B]]
扩展导入*.all._并没有改变编译器错误.
我在scala 2.11.8上,因为项目所依赖的所有库都没有发布2.12版本
为了方便读者,已经整理了注释中的答案:
1:cats.Functor由@wheaties和@ jean-philippe-pellet提供,您可以在简单的scala中使用它(此处使用定义)
def eitherFmap[F[_], A, B](either: Either[F[A], F[B])(implicit F: Functor[F]): F[Either[A, B]] =
either.fold(f => F.map(f)(Left(_)), f => F.map(f)(Right(_)))
Run Code Online (Sandbox Code Playgroud)
2:bisequence由于进口冲突,猫拒绝编译:这里一次进口就足够了-@kolmar提供
import cats.instances._
...
val futureOfEither = eitherOfFutures.bisequence
Run Code Online (Sandbox Code Playgroud)
3:阅读猫的进口指南以避免将来出现此问题-@ peter-neyens提供
| 归档时间: |
|
| 查看次数: |
1581 次 |
| 最近记录: |