From 46c513d4a07c737b9a0c71a41b13238146419b7c Mon Sep 17 00:00:00 2001 From: Luka Jacobowitz Date: Mon, 20 Aug 2018 20:41:37 +0200 Subject: [PATCH] Small tweak --- core/src/main/scala/cats/data/EitherT.scala | 3 +- core/src/main/scala/cats/syntax/all.scala | 1 + core/src/main/scala/cats/syntax/either.scala | 2 +- core/src/main/scala/cats/syntax/package.scala | 2 +- .../main/scala/cats/syntax/validated.scala | 33 ++++++++++++++++++- .../test/scala/cats/tests/EitherTSuite.scala | 6 ++++ 6 files changed, 43 insertions(+), 4 deletions(-) diff --git a/core/src/main/scala/cats/data/EitherT.scala b/core/src/main/scala/cats/data/EitherT.scala index 7c5d6937b4..7dc39d170a 100644 --- a/core/src/main/scala/cats/data/EitherT.scala +++ b/core/src/main/scala/cats/data/EitherT.scala @@ -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]]`. * diff --git a/core/src/main/scala/cats/syntax/all.scala b/core/src/main/scala/cats/syntax/all.scala index 4cae02b81f..78a67f51df 100644 --- a/core/src/main/scala/cats/syntax/all.scala +++ b/core/src/main/scala/cats/syntax/all.scala @@ -74,3 +74,4 @@ trait AllSyntaxBinCompat2 extends ParallelTraverseSyntax with EitherSyntaxBinCompat0 with ListSyntaxBinCompat0 + with ValidatedSyntaxBincompat0 diff --git a/core/src/main/scala/cats/syntax/either.scala b/core/src/main/scala/cats/syntax/either.scala index 2f29d8d6e2..78fcfc7863 100644 --- a/core/src/main/scala/cats/syntax/either.scala +++ b/core/src/main/scala/cats/syntax/either.scala @@ -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) } diff --git a/core/src/main/scala/cats/syntax/package.scala b/core/src/main/scala/cats/syntax/package.scala index 5a11cc5448..03b4413f22 100644 --- a/core/src/main/scala/cats/syntax/package.scala +++ b/core/src/main/scala/cats/syntax/package.scala @@ -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 diff --git a/core/src/main/scala/cats/syntax/validated.scala b/core/src/main/scala/cats/syntax/validated.scala index fef761d817..8b12f636b2 100644 --- a/core/src/main/scala/cats/syntax/validated.scala +++ b/core/src/main/scala/cats/syntax/validated.scala @@ -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) @@ -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) +} diff --git a/tests/src/test/scala/cats/tests/EitherTSuite.scala b/tests/src/test/scala/cats/tests/EitherTSuite.scala index d89b681c62..e87400d1c9 100644 --- a/tests/src/test/scala/cats/tests/EitherTSuite.scala +++ b/tests/src/test/scala/cats/tests/EitherTSuite.scala @@ -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)