Skip to content

Commit

Permalink
Small tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
Luka Jacobowitz committed Aug 21, 2018
1 parent 1f16bb8 commit 46c513d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 4 deletions.
3 changes: 2 additions & 1 deletion core/src/main/scala/cats/data/EitherT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ final case class EitherT[F[_], A, B](value: F[Either[A, B]]) {
def toValidatedNel(implicit F: Functor[F]): F[ValidatedNel[A, B]] =
F.map(value)(_.toValidatedNel)

def toValidatedNec(implicit F: Functor[F]): F[ValidatedNec[A, B]] = ???
def toValidatedNec(implicit F: Functor[F]): F[ValidatedNec[A, B]] =
F.map(value)(_.toValidatedNec)

/** Run this value as a `[[Validated]]` against the function and convert it back to an `[[EitherT]]`.
*
Expand Down
1 change: 1 addition & 0 deletions core/src/main/scala/cats/syntax/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,4 @@ trait AllSyntaxBinCompat2
extends ParallelTraverseSyntax
with EitherSyntaxBinCompat0
with ListSyntaxBinCompat0
with ValidatedSyntaxBincompat0
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/syntax/either.scala
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ final class EitherIdOpsBinCompat0[A](val value: A) extends AnyVal {
final class EitherOpsBinCompat0[A, B](val value: Either[A, B]) extends AnyVal {
/** Returns a [[cats.data.ValidatedNec]] representation of this disjunction with the `Left` value
* as a single element on the `Invalid` side of the [[cats.data.NonEmptyList]]. */
def toValidatedNec[AA >: A]: ValidatedNec[AA, B] = value match {
def toValidatedNec: ValidatedNec[A, B] = value match {
case Left(a) => Validated.invalidNec(a)
case Right(b) => Validated.valid(b)
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/syntax/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ package object syntax {
object traverse extends TraverseSyntax
object nonEmptyTraverse extends NonEmptyTraverseSyntax
object unorderedTraverse extends UnorderedTraverseSyntax
object validated extends ValidatedSyntax with ValidatedExtensionSyntax
object validated extends ValidatedSyntax with ValidatedExtensionSyntax with ValidatedSyntaxBincompat0
object vector extends VectorSyntax
object writer extends WriterSyntax
object set extends SetSyntax
Expand Down
33 changes: 32 additions & 1 deletion core/src/main/scala/cats/syntax/validated.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cats
package syntax

import cats.data.{ Validated, ValidatedNel }
import cats.data.{Validated, ValidatedNec, ValidatedNel}

trait ValidatedSyntax {
implicit final def catsSyntaxValidatedId[A](a: A): ValidatedIdSyntax[A] = new ValidatedIdSyntax(a)
Expand All @@ -23,3 +23,34 @@ final class ValidatedExtension[E, A](val self: Validated[E, A]) extends AnyVal {
def liftTo[F[_]](implicit F: ApplicativeError[F, E]): F[A] =
new ApplicativeErrorExtensionOps(F).fromValidated(self)
}

trait ValidatedSyntaxBincompat0 {
implicit final def catsSyntaxValidatedIdBinCompat0[A](a: A): ValidatedIdOpsBinCompat0[A] =
new ValidatedIdOpsBinCompat0(a)
}

final class ValidatedIdOpsBinCompat0[A](val a: A) extends AnyVal {
/**
* Wrap a value to a valid ValidatedNec
*
* For example:
* {{{
* scala> import cats.implicits._, cats.data._
* scala> 1.validNec[String]
* res0: Validated[NonEmptyChain[String], Int] = Valid(1)
* }}}
*/
def validNec[B]: ValidatedNec[B, A] = Validated.Valid(a)

/**
* Wrap a value to an invalid ValidatedNec
*
* For example:
* {{{
* scala> import cats.implicits._, cats.data._
* scala> "Err".invalidNec[Int]
* res0: Validated[NonEmptyChain[String], Int] = Invalid(Chain(Err))
* }}}
*/
def invalidNec[B]: ValidatedNec[A, B] = Validated.invalidNec(a)
}
6 changes: 6 additions & 0 deletions tests/src/test/scala/cats/tests/EitherTSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ class EitherTSuite extends CatsSuite {
}
}

test("toValidatedNec") {
forAll { (eithert: EitherT[List, String, Int]) =>
eithert.toValidatedNec.map(_.toEither.leftMap(_.head)) should === (eithert.value)
}
}

test("toNested") {
forAll { (eithert: EitherT[List, String, Int]) =>
eithert.toNested.value should === (eithert.value)
Expand Down

0 comments on commit 46c513d

Please sign in to comment.