Skip to content

Commit

Permalink
Merge pull request #1658 from edmundnoble/validNel
Browse files Browse the repository at this point in the history
Add Validated.validNel
  • Loading branch information
ceedubs authored May 13, 2017
2 parents 9f98924 + a23da34 commit fc40a1c
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions core/src/main/scala/cats/data/Validated.scala
Original file line number Diff line number Diff line change
Expand Up @@ -425,12 +425,49 @@ private[data] sealed abstract class ValidatedInstances2 {
}

private[data] trait ValidatedFunctions {
/**
* Converts an `A` to a `Validated[A, B]`.
*
* For example:
* {{{
* scala> Validated.invalid[IllegalArgumentException, String](new IllegalArgumentException("Argument is nonzero"))
* res0: Validated[IllegalArgumentException, String] = Invalid(java.lang.IllegalArgumentException: Argument is nonzero)
* }}}
*/
def invalid[A, B](a: A): Validated[A, B] = Validated.Invalid(a)

/**
* Converts an `A` to a `ValidatedNel[A, B]`.
*
* For example:
* {{{
* scala> Validated.invalidNel[IllegalArgumentException, String](new IllegalArgumentException("Argument is nonzero"))
* res0: ValidatedNel[IllegalArgumentException, String] = Invalid(NonEmptyList(java.lang.IllegalArgumentException: Argument is nonzero))
* }}}
*/
def invalidNel[A, B](a: A): ValidatedNel[A, B] = Validated.Invalid(NonEmptyList(a, Nil))

/**
* Converts a `B` to a `Validated[A, B]`.
*
* For example:
* {{{
* scala> Validated.valid[IllegalArgumentException, String]("Hello world")
* res0: Validated[IllegalArgumentException, String] = Valid(Hello world)
* }}}
*/
def valid[A, B](b: B): Validated[A, B] = Validated.Valid(b)

/**
* Converts a `B` to a `ValidatedNel[A, B]`.
*
* For example:
* {{{
* scala> Validated.validNel[IllegalArgumentException, String]("Hello world")
* res0: ValidatedNel[IllegalArgumentException, String] = Valid(Hello world)
* }}}
*/
def validNel[A, B](b: B): ValidatedNel[A, B] = Validated.Valid(b)

def catchNonFatal[A](f: => A): Validated[Throwable, A] =
try {
Expand Down

0 comments on commit fc40a1c

Please sign in to comment.