Here I see two possible solutions. Probably the easiest one using pattern matching in the argument, for example:
def convert[A](v: Option[Validation[Throwable, A]]): Validation[Throwable, Option[A]] = { v match { case None => Validation.success(None) case Some(valid) => valid.map(Some(_)) } }
For a Scalaz-based solution, I was thinking about consistency , so you need to use ValidationNel instead of checking, to aggregate possible problems, you can implement convert with Traversable :
def convert[A](v: Option[ValidationNel[Throwable, A]]): ValidationNel[Throwable, Option[A]] = Traverse[Option].sequenceU(v)
Please note that in fact I use sequenceU instead of just sequence , it is nothing but Scalaz's internal magic for the correct type inference, because validation has two types of parameters. Hope this helps.
source share