Skip to content

Commit

Permalink
Merge pull request #2555 from Free2MoveApp/add-nonemptychain-to-optio…
Browse files Browse the repository at this point in the history
…nops

Add OptionOps.toInvalidNec and OptionOps.toValidNec
  • Loading branch information
ceedubs committed Oct 14, 2018
2 parents 82a57be + 1942073 commit 191fae4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ Here's a (non-exhaustive) list of companies that use Cats in production. Don't s
- [e.near](http://enear.co)
- [E.ON](https://eon.com)
- [formation.ai](https://formation.ai)
- [Free2Move](https://free2move.com)
- [HomeAway](https://www.homeaway.com)
- [iHeartRadio](https://iheart.com)
- [ITV](https://www.itv.com/)
Expand Down
45 changes: 44 additions & 1 deletion core/src/main/scala/cats/syntax/option.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cats
package syntax

import cats.data.{Ior, OptionT, Validated, ValidatedNel}
import cats.data.{Ior, OptionT, Validated, ValidatedNel, ValidatedNec}
import cats.syntax.OptionOps.LiftToPartiallyApplied

trait OptionSyntax {
Expand Down Expand Up @@ -71,6 +71,28 @@ final class OptionOps[A](val oa: Option[A]) extends AnyVal {
*/
def toInvalidNel[B](b: => B): ValidatedNel[A, B] = oa.fold[ValidatedNel[A, B]](Validated.Valid(b))(Validated.invalidNel)

/**
* If the `Option` is a `Some`, wrap its value in a [[cats.data.Chain]]
* and return it in a [[cats.data.Validated.Invalid]].
* If the `Option` is `None`, return the provided `B` value in a
* [[cats.data.Validated.Valid]].
*
* Example:
* {{{
* scala> import cats.data.ValidatedNec
* scala> import cats.implicits._
*
* scala> val error1: Option[String] = Some("error!")
* scala> error1.toInvalidNec(3)
* res0: ValidatedNec[String, Int] = Invalid(Chain(error!))
*
* scala> val error2: Option[String] = None
* scala> error2.toInvalidNec(3)
* res1: ValidatedNec[String, Int] = Valid(3)
* }}}
*/
def toInvalidNec[B](b: => B): ValidatedNec[A, B] = oa.fold[ValidatedNec[A, B]](Validated.Valid(b))(Validated.invalidNec)

/**
* If the `Option` is a `Some`, return its value in a [[cats.data.Validated.Valid]].
* If the `Option` is `None`, return the provided `B` value in a
Expand Down Expand Up @@ -113,6 +135,27 @@ final class OptionOps[A](val oa: Option[A]) extends AnyVal {
*/
def toValidNel[B](b: => B): ValidatedNel[B, A] = oa.fold[ValidatedNel[B, A]](Validated.invalidNel(b))(Validated.Valid(_))

/**
* If the `Option` is a `Some`, return its value in a [[cats.data.Validated.Valid]].
* If the `Option` is `None`, wrap the provided `B` value in a [[cats.data.Chain]]
* and return the result in a [[cats.data.Validated.Invalid]].
*
* Example:
* {{{
* scala> import cats.data.ValidatedNec
* scala> import cats.implicits._
*
* scala> val result1: Option[Int] = Some(3)
* scala> result1.toValidNec("error!")
* res0: ValidatedNec[String, Int] = Valid(3)
*
* scala> val result2: Option[Int] = None
* scala> result2.toValidNec("error!")
* res1: ValidatedNec[String, Int] = Invalid(Chain(error!))
* }}}
*/
def toValidNec[B](b: => B): ValidatedNec[B, A] = oa.fold[ValidatedNec[B, A]](Validated.invalidNec(b))(Validated.Valid(_))

/**
* If the `Option` is a `Some`, return its value in a [[cats.data.Ior.Right]].
* If the `Option` is `None`, wrap the provided `B` value in a [[cats.data.Ior.Left]]
Expand Down

0 comments on commit 191fae4

Please sign in to comment.