How to compose the future of any of them / disjunction in Scala

Suppose I have the following functions:

 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 is mayFail

slow due slowAndMayFail

, so I would like it to return the future Error \/ Int

.

The direct approach is probably to do a slowAndMayFail

return scalaz.Future[Error\/Int]]

, make all other functions ( mayFail1

, mayFail2

etc.) return Future[Error\/Int]]

, and then add EitherT

them to link them.

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

I know what Taskscalaz

provides , but I'm sticking with it for now .Future

+3


source to share





All Articles