问题
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