Could not find Traverse for sequencing Seq [ValidationNel [String, MyCaseClass]] => ValidationNel [String, Seq [MyCaseClass]]
I have code like the following:
import scalaz._
import Scalaz._
case class Foo(i: Int)
type ValidatedNel[A] = ValidationNel[String, A]
val foos: Seq[ValidatedNel[Foo]] = Seq(Success(Foo(1)), Success(Foo(2)), Failure(NonEmptyList("3 failed")), Failure(NonEmptyList("4 failed")))
val validated: ValidatedNel[Seq[Foo]] = foos.sequence[ValidatedNel, Foo]
It does not happen at compile time with this error:
Error: (51, 50) Could not find implicit value for parameter F0: scalaz.Traverse [Seq] val validated: ValidatedNel [Seq [Foo]] = foos.sequence [ValidatedNel, Foo]
Error: (51, 50) Insufficient arguments for ToTraverseOps: (implicit F0: scalaz.Traverse [Seq]) scalaz.syntax.TraverseOps [Seq, scalaz.package.ValidationNel [String, Foo]]. Undefined parameter value F0. val validated: ValidatedNel [Seq [Foo]] = foos.sequence [ValidatedNel, Foo]
I would like to get the end result like this in the example I gave:
val validated = Failure(NonEmptyList("3 failed", "4 failed"))
If you foos
only had Success
not Failure
, I would like to see them a simple sequence Success(Foo(1), Foo(2))
.
Why am I getting the compilation failure I mentioned? As I understand it, this should work based on types.
Does it have to do with unpacking like aliasing with something like A[B[C[D], E]] => B[C[D, A[E]]
instead A[B[C]] => B[A[C]]
?
source to share
AFAIK, there is no instance in scalaz Traverse[Seq]
. You can replace Seq
to List
or convert it, or implement Traverse[Seq]
( implicit val seqInstance: Traverse[Seq] = ???
).
import scalaz._
import Scalaz._
case class Foo(i: Int)
type ValidatedNel[A] = ValidationNel[String, A]
val foos: List[ValidatedNel[Foo]] = List(Success(Foo(1)), Success(Foo(2)), Failure(NonEmptyList("3 failed")), Failure(NonEmptyList("4 failed")))
val validated: ValidatedNel[List[Foo]] = foos.sequence[ValidatedNel, Foo]
source to share