Skip to content

Instantly share code, notes, and snippets.

@kubukoz
Last active July 12, 2017 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kubukoz/e5946a996c25a2b23fbfb225ebb722fe to your computer and use it in GitHub Desktop.
Save kubukoz/e5946a996c25a2b23fbfb225ebb722fe to your computer and use it in GitHub Desktop.
import scala.language.higherKinds
import scalaz.{Semigroup, Traverse, Validation}
object ValidationTraverseOps {
implicit class ValidationTraverseOps[G[_], T](val gt: G[T]) extends AnyVal {
def traverseRight[U, E : Semigroup](fun: T => Validation[E, U])(implicit traverseG: Traverse[G]): Validation[E, G[U]] = {
traverseG.traverse[({type L[TT] = Validation[E, TT]})#L, T, U](gt)(fun)
}
}
//sample
import scalaz.syntax.validation._
import scalaz.std.option._
import scalaz.ValidationNel
val opt: Option[Int] = Some(5)
def validate(s: Int): ValidationNel[String, Int] = s.success.ensure("too much")(_ <= 2).toValidationNel
val result: ValidationNel[String, Option[Int]] = opt.traverseRight(validate)
}
@kubukoz
Copy link
Author

kubukoz commented Jul 12, 2017

This is, in fact, the same as traverseU, except made concrete for the * -> * -> * kind, Validation in specific. We removed this syntactic class and replaced all usages with traverseU, which I recomment to anyone that might be, or want to be, using this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment