Skip to content

Instantly share code, notes, and snippets.

@miguel-vila
Last active October 16, 2015 18:48
Show Gist options
  • Save miguel-vila/5ccbd8daa825a413228d to your computer and use it in GitHub Desktop.
Save miguel-vila/5ccbd8daa825a413228d to your computer and use it in GitHub Desktop.
scala> import scalaz.std.list.listInstance
import scalaz.std.list.listInstance
scala> import scalaz.Traverse
import scalaz.Traverse
scala> import scalaz.ValidationNel
import scalaz.ValidationNel
scala> import scalaz.Validation
import scalaz.Validation
scala> case class MyError(msg: String)
defined class MyError
scala> case class MyValue(n: Int)
defined class MyValue
scala> val valids = List( Validation.success( MyValue(1) ) , Validation.failureNel( MyError("err1" ) ) , Validation.success( MyValue(2) ) , Validation.failureNel( MyError("err2") ) )
valids: List[scalaz.Validation[scalaz.NonEmptyList[MyError],MyValue]] = List(Success(MyValue(1)), Failure(NonEmptyList(MyError(err1))), Success(MyValue(2)), Failure(NonEmptyList(MyError(err2))))
scala> Traverse[List].sequence( valids )
<console>:17: error: no type parameters for method sequence: (fga: List[G[A]])(implicit evidence$7: scalaz.Applicative[G])G[List[A]] exist so that it can be applied to arguments (List[scalaz.Validation[scalaz.NonEmptyList[MyError],MyValue]])
--- because ---
argument expression's type is not compatible with formal parameter type;
found : List[scalaz.Validation[scalaz.NonEmptyList[MyError],MyValue]]
required: List[?G[?A]]
Traverse[List].sequence( valids )
^
<console>:17: error: type mismatch;
found : List[scalaz.Validation[scalaz.NonEmptyList[MyError],MyValue]]
required: List[G[A]]
Traverse[List].sequence( valids )
^
<console>:17: error: could not find implicit value for evidence parameter of type scalaz.Applicative[G]
Traverse[List].sequence( valids )
^
// Scala no puede deducir los tipos automáticamente, por lo que para llamar sequence toca poner los tipos explícitos:
scala> valids.sequence[({ type F[X] = ValidationNel[MyError, X] })#F, MyValue] // Esa cosa es un type lambda
res7: scalaz.Validation[scalaz.NonEmptyList[MyError],List[MyValue]] = Failure(NonEmptyList(MyError(err1), MyError(err2)))
// Para no hacer esa cochinada está sequenceU que si permite que scala deduzca los tipos
scala> Traverse[List].sequenceU( valids )
res1: scalaz.Validation[scalaz.NonEmptyList[MyError],List[MyValue]] = Failure(NonEmptyList(MyError(err1), MyError(err2)))
scala> val valids2: List[ValidationNel[MyError,MyValue]] = List( Validation.success( MyValue(1) ) , Validation.success( MyValue(2) ) )
valids2: List[scalaz.ValidationNel[MyError,MyValue]] = List(Success(MyValue(1)), Success(MyValue(2)))
scala> Traverse[List].sequenceU( valids2 )
res3: scalaz.Validation[scalaz.NonEmptyList[MyError],List[MyValue]] = Success(List(MyValue(1), MyValue(2)))
scala> import scalaz.syntax.traverse._
import scalaz.syntax.traverse._
scala> valids.sequenceU
res4: scalaz.Validation[scalaz.NonEmptyList[MyError],List[MyValue]] = Failure(NonEmptyList(MyError(err1), MyError(err2)))
scala> valids2.sequenceU
res5: scalaz.Validation[scalaz.NonEmptyList[MyError],List[MyValue]] = Success(List(MyValue(1), MyValue(2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment