Skip to content

Commit

Permalink
added leftNel and rightNel syntax (#2146)
Browse files Browse the repository at this point in the history
* added leftNel and rightNel syntax

* rm whitespace

* Update either.scala
  • Loading branch information
kailuowang authored Jan 13, 2018
1 parent 75052a0 commit 7e6336f
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion core/src/main/scala/cats/syntax/either.scala
Original file line number Diff line number Diff line change
@@ -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._
Expand Down Expand Up @@ -334,6 +334,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.one(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. */
Expand Down

0 comments on commit 7e6336f

Please sign in to comment.