I'm going to assume that by Validation(T)
you mean something like ValidationNel[Throwable, T]
, since Validation[T]
isn't anything and Validation[E, T]
doesn't have an applicative functor instance unless E
has a semigroup instance.
What you're looking for is probably traverse
(or traverseU
if you want to avoid writing out the type parameters). You can write the following, for example:
scala> case class Foo(singleVal: ValidationNel[Throwable, String])
defined class Foo
scala> val x = some(Foo("hey".success))
x: Option[Foo] = Some(Foo(Success(hey)))
scala> val y = none[Foo]
y: Option[Foo] = None
scala> println(x.traverseU(_.singleVal))
Success(Some(hey))
scala> println(y.traverseU(_.singleVal))
Success(None)
In general if M
has a Traverse
instance and N
has an applicative functor instance, you can transform a M[A]
into a N[M[B]]
given a function A => N[B]
with traverse
(see my answer here for some additional discussion).