How to compose Future of Either/Disjunction in Scala

岁酱吖の 提交于 2019-12-10 18:46:03

问题


Suppose I have the following functions to compose:

 val mayFail1: Int => Error \/ Int          = ???
 val slowAndMayFail: Int => Error \/ String = ???
 val mayFail2: String => Error \/ Int       = ???
 val mayFail3: String => Error \/ Int       = ???

 val mayFail: Int => Error \/ Int = {x =>
   for {
     x1 <- mayFail1(x)
     s  <- slowAndMayFail(x1)
     x2 <- mayFail2(s)
     x3 <- mayFail3(x2)         
   } yield x3
 }

The function mayFail is slow because of slowAndMayFail, so I would like it to return a future of Error \/ Int.

The straightforward approach is probably to make slowAndMayFail return scalaz.Future[Error\/Int]], make all other functions (mayFail1, mayFail2, etc.) return Future[Error\/Int]] as well and then add EitherT to compose them together.

Does it make sense ? Are there easier/simpler solutions ?

I know that scalaz provides Task but I am sticking with Future for now.

来源:https://stackoverflow.com/questions/31140753/how-to-compose-future-of-either-disjunction-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!