Async computation with Validation in Scala using Scalaz

后端 未结 1 1592
清歌不尽
清歌不尽 2021-02-06 07:29

Being writing a completely async library to access a remote service (using Play2.0), I\'m using Promise and Validation to create non-blocking call, whi

1条回答
  •  醉梦人生
    2021-02-06 07:59

    The concept you are looking for here is monad transformers. In brief, monad transformers compensate for monads not composing by allowing you to "stack" them.

    You didn't mention the version of Scalaz you are using, but if you look in the scalaz-seven branch, you'll find ValidationT. This can be used to wrap any F[Validation[E, A]] into a ValidationT[F, E, A], where in your case F = Promise. If you change f and g to return ValidationT, then you can leave your code as

    for {
      x ← f(a)
      y ← g(b)
    } yield y
    

    This will give you a ValidationT[Promise, E, B] as a result.

    0 讨论(0)
提交回复
热议问题