From a6ffd00050287830b9929a9220683b0ad879f2a9 Mon Sep 17 00:00:00 2001 From: Kailuo Wang Date: Wed, 10 Jan 2018 14:49:01 -0500 Subject: [PATCH 1/3] added leftNel and rightNel syntax --- core/src/main/scala/cats/syntax/either.scala | 27 +++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/core/src/main/scala/cats/syntax/either.scala b/core/src/main/scala/cats/syntax/either.scala index bad47ec411..e2d054f755 100644 --- a/core/src/main/scala/cats/syntax/either.scala +++ b/core/src/main/scala/cats/syntax/either.scala @@ -1,7 +1,7 @@ package cats package syntax -import cats.data.{EitherT, Ior, Validated, ValidatedNel} +import cats.data.{EitherT, Ior, NonEmptyList, Validated, ValidatedNel} import scala.reflect.ClassTag import scala.util.{Failure, Success, Try} import EitherSyntax._ @@ -329,6 +329,31 @@ final class EitherIdOps[A](val obj: A) extends AnyVal { /** Wrap a value in `Right`. */ def asRight[B]: Either[B, A] = Right(obj) + + /** + * Wrap a value to a left EitherNel + * + * For example: + * {{{ + * scala> import cats.implicits._, cats.data.NonEmptyList + * scala> "Err".leftNel[Int] + * res0: Either[NonEmptyList[String], Int] = Left(NonEmptyList(Err)) + * }}} + */ + def leftNel[B]: Either[NonEmptyList[A], B] = Left(NonEmptyList.of(obj)) + + /** + * Wrap a value to a right EitherNel + * + * For example: + * {{{ + * scala> import cats.implicits._, cats.data.NonEmptyList + * scala> 1.rightNel[String] + * res0: Either[NonEmptyList[String], Int] = Right(1) + * }}} + */ + def rightNel[B]: Either[NonEmptyList[B], A] = Right(obj) + } /** Convenience methods to use `Either` syntax inside `Either` syntax definitions. */ From 60e9227133a533b015277a33c1c91fda4d89bb7f Mon Sep 17 00:00:00 2001 From: "Kai(luo) Wang" Date: Wed, 10 Jan 2018 16:03:37 -0500 Subject: [PATCH 2/3] rm whitespace --- core/src/main/scala/cats/syntax/either.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/scala/cats/syntax/either.scala b/core/src/main/scala/cats/syntax/either.scala index e2d054f755..15040ba021 100644 --- a/core/src/main/scala/cats/syntax/either.scala +++ b/core/src/main/scala/cats/syntax/either.scala @@ -336,7 +336,7 @@ final class EitherIdOps[A](val obj: A) extends AnyVal { * For example: * {{{ * scala> import cats.implicits._, cats.data.NonEmptyList - * scala> "Err".leftNel[Int] + * scala> "Err".leftNel[Int] * res0: Either[NonEmptyList[String], Int] = Left(NonEmptyList(Err)) * }}} */ @@ -348,7 +348,7 @@ final class EitherIdOps[A](val obj: A) extends AnyVal { * For example: * {{{ * scala> import cats.implicits._, cats.data.NonEmptyList - * scala> 1.rightNel[String] + * scala> 1.rightNel[String] * res0: Either[NonEmptyList[String], Int] = Right(1) * }}} */ From 892c8fc4826eb4d8aca07ebb9fa19c660158880f Mon Sep 17 00:00:00 2001 From: "Kai(luo) Wang" Date: Fri, 12 Jan 2018 12:36:53 -0500 Subject: [PATCH 3/3] Update either.scala --- core/src/main/scala/cats/syntax/either.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/scala/cats/syntax/either.scala b/core/src/main/scala/cats/syntax/either.scala index 15040ba021..4c15198d93 100644 --- a/core/src/main/scala/cats/syntax/either.scala +++ b/core/src/main/scala/cats/syntax/either.scala @@ -340,7 +340,7 @@ final class EitherIdOps[A](val obj: A) extends AnyVal { * res0: Either[NonEmptyList[String], Int] = Left(NonEmptyList(Err)) * }}} */ - def leftNel[B]: Either[NonEmptyList[A], B] = Left(NonEmptyList.of(obj)) + def leftNel[B]: Either[NonEmptyList[A], B] = Left(NonEmptyList.one(obj)) /** * Wrap a value to a right EitherNel