Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find Traverse for sequencing Seq[ValidationNel[String, MyCaseClass]] => ValidationNel[String, Seq[MyCaseClass]]

I have some 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]

This fails during compilation 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) not enough arguments for method ToTraverseOps: (implicit F0: scalaz.Traverse[Seq])scalaz.syntax.TraverseOps[Seq,scalaz.package.ValidationNel[String,Foo]]. Unspecified value parameter F0. val validated: ValidatedNel[Seq[Foo]] = foos.sequence[ValidatedNel, Foo]

I'd like to get an end result like this in the example I gave:

val validated = Failure(NonEmptyList("3 failed", "4 failed"))

If foos had only Success and no Failure, I'd like to see the simple sequence of them: Success(Foo(1), Foo(2)).

Why am I getting the compile failure I mentioned? As far as I understand it, this should work based on the types.

Is this related to the type aliasing unpacking to something like A[B[C[D], E]] => B[C[D, A[E]] instead of A[B[C]] => B[A[C]]?

like image 434
Daenyth Avatar asked Jul 05 '26 14:07

Daenyth


1 Answers

AFAIK, there is no instance of Traverse[Seq] in scalaz. You may replace the Seq by a 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]
like image 158
Dimitri Avatar answered Jul 07 '26 10:07

Dimitri