-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ior syntax #1540
Ior syntax #1540
Changes from all commits
826b211
756d1ed
4610791
50bf4a6
3eb78f8
4350f57
edf7466
2b18941
54fb5c8
831e32f
9e71a77
6924628
f03ab7d
989de6c
0922773
d650b09
12f7e31
b90a962
ef82db6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,11 @@ sealed abstract class Validated[+E, +A] extends Product with Serializable { | |
*/ | ||
def toOption: Option[A] = fold(_ => None, Some.apply) | ||
|
||
/** | ||
* Returns Valid values wrapped in Ior.Right, and None for Ior.Left values | ||
*/ | ||
def toIor: Ior[E, A] = fold(Ior.left, Ior.right) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
/** | ||
* Convert this value to a single element List if it is Valid, | ||
* otherwise return an empty List | ||
|
@@ -430,13 +435,18 @@ private[data] trait ValidatedFunctions { | |
} | ||
|
||
/** | ||
* Converts an `Either[A, B]` to an `Validated[A, B]`. | ||
* Converts an `Either[A, B]` to a `Validated[A, B]`. | ||
*/ | ||
def fromEither[A, B](e: Either[A, B]): Validated[A, B] = e.fold(invalid, valid) | ||
|
||
/** | ||
* Converts an `Option[B]` to an `Validated[A, B]`, where the provided `ifNone` values is returned on | ||
* Converts an `Option[B]` to a `Validated[A, B]`, where the provided `ifNone` values is returned on | ||
* the invalid of the `Validated` when the specified `Option` is `None`. | ||
*/ | ||
def fromOption[A, B](o: Option[B], ifNone: => A): Validated[A, B] = o.fold(invalid[A, B](ifNone))(valid) | ||
|
||
/** | ||
* Converts an `Ior[A, B]` to a `Validated[A, B]`. | ||
*/ | ||
def fromIor[A, B](ior: Ior[A, B]): Validated[A, B] = ior.fold(invalid, valid, (_, b) => valid(b)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cats.syntax | ||
|
||
import cats.data.Ior | ||
|
||
trait IorSyntax { | ||
implicit def catsSyntaxIorId[A](a: A): IorIdOps[A] = new IorIdOps(a) | ||
} | ||
|
||
final class IorIdOps[A](val a: A) extends AnyVal { | ||
/** | ||
* Wrap a value in `Ior.Right`. | ||
* | ||
* Example: | ||
* {{{ | ||
* scala> import cats.data.Ior | ||
* scala> import cats.implicits._ | ||
* | ||
* scala> "hello".rightIor[String] | ||
* res0: Ior[String, String] = Right(hello) | ||
* }}} | ||
*/ | ||
def rightIor[B]: Ior[B, A] = Ior.right(a) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Context in Either instances uses There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's only that way because of the |
||
|
||
/** | ||
* Wrap a value in `Ior.Left`. | ||
* | ||
* Example: | ||
* {{{ | ||
* scala> import cats.data.Ior | ||
* scala> import cats.implicits._ | ||
* | ||
* scala> "error".leftIor[String] | ||
* res0: Ior[String, String] = Left(error) | ||
* }}} | ||
*/ | ||
def leftIor[B]: Ior[A, B] = Ior.left(a) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍