Last active
July 12, 2017 16:33
-
-
Save kubukoz/e5946a996c25a2b23fbfb225ebb722fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 withtraverseU
, which I recomment to anyone that might be, or want to be, using this.