-
-
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
Add docs for Ior #1822
Add docs for Ior #1822
Changes from 1 commit
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 |
---|---|---|
@@ -0,0 +1,114 @@ | ||
--- | ||
layout: docs | ||
title: "Ior" | ||
section: "data" | ||
source: "core/src/main/scala/cats/data/Ior.scala" | ||
scaladoc: "#cats.data.Ior" | ||
--- | ||
# Ior | ||
|
||
`Ior` represents an inclusive-or relationship between two data types. | ||
This makes it very similar to the [`Either`](either.html) data type, which represents an `Xor` relationship. | ||
What this means, is that an `Ior[A, B]` (also written as `A Ior B`) can contain either an `A`, a `B`, or both an `A` and `B`. | ||
Another similarity to `Either` is that `Ior` is right-biased, | ||
which means that the `map` and `flatMap` functions will work on the right side of the `Ior`, in our case the `B` value. | ||
You can see this in the function signature of `map`: | ||
|
||
```scala | ||
def map[B, C](fa: A Ior B)(f: B => C): A Ior C | ||
``` | ||
|
||
We can create `Ior` values using `Ior.left`, `Ior.right` and `Ior.both`: | ||
|
||
```tut | ||
import cats.data.Ior | ||
|
||
val right = Ior.right[String, Int](3) | ||
|
||
val left = Ior.left[String, Int]("Error") | ||
|
||
val both = Ior.both("Warning", 3) | ||
``` | ||
|
||
Cats also offers syntax enrichment for `Ior`. The `leftIor` and `rightIor` functions can be imported from `cats.syntax.ior._`: | ||
|
||
```tut | ||
import cats.syntax.ior._ | ||
|
||
val right = 3.rightIor | ||
|
||
val left = "Error".leftIor | ||
``` | ||
|
||
|
||
When we look at the `Monad` or `Applicative` instances of `Ior`, we can see that they actually requires a `Semigroup` instance on the left side. | ||
This is because `Ior` will actually accumulate failures on the left side, very similar to how the [`Validated`](validated.html) data type does. | ||
This means we can accumulate data on the left side while also being able to short-circuit upon the first right-side-only value. | ||
For example, sometimes, we might want to accumulate warnings together with a valid result and only halt the computation on a "hard error" | ||
Here's an example of how we might be able to do that: | ||
|
||
```tut:silent | ||
import cats.implicits._ | ||
import cats.data.{ NonEmptyList => Nel } | ||
|
||
type Failures = Nel[String] | ||
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. Not sure if we want to introduce that here, but there also is a |
||
|
||
case class Username(value: String) extends AnyVal | ||
case class Password(value: String) extends AnyVal | ||
|
||
case class User(name: Username, pw: Password) | ||
|
||
def validateUsername(u: String): Failures Ior Username = { | ||
if (u.isEmpty) | ||
Nel.of("Can't be empty").leftIor | ||
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. Without the |
||
else if (u.contains(".")) | ||
Ior.both(Nel.of("Dot in name is deprecated"), Username(u)) | ||
else | ||
Username(u).rightIor | ||
} | ||
|
||
def validatePassword(p: String): Failures Ior Password = { | ||
if (p.length < 8) | ||
Nel.of("Password too short").leftIor | ||
else if (p.length < 10) | ||
Ior.both(Nel.of("Password should be longer"), Password(p)) | ||
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. If you are creating a one element |
||
else | ||
Password(p).rightIor | ||
} | ||
|
||
def validateUser(name: String, password: String): Failures Ior User = | ||
(validateUsername(name), validatePassword(password)).mapN(User) | ||
|
||
``` | ||
|
||
Now we're able to validate user data and also accumulate non-fatal warnings: | ||
|
||
```tut | ||
|
||
validateUser("John", "password12") | ||
|
||
validateUser("john.doe", "password") | ||
|
||
validateUser("jane", "short") | ||
|
||
``` | ||
|
||
To extract the values, we can use the `fold` method, which expects a function for each case the `Ior` can represent: | ||
|
||
```tut | ||
|
||
validateUser("john.doe", "password").fold( | ||
errorNel => s"Error: ${errorNel.head}", | ||
user => s"Success: $user", | ||
(warnings, user) => s"Warning: ${user.name.value}; The following warnings occurred: ${warnings.show}" | ||
) | ||
|
||
``` | ||
|
||
|
||
We can also convert our `Ior` to `Either`, `Validated` or `Option`. | ||
All of these conversions will discard the left side value if both are available: | ||
|
||
```tut | ||
Ior.both("Warning", 42).toEither | ||
``` |
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.
I'm a bit hesitant about this
Xor
reference. I think that this might be confusing, as Cats no longer has anXor
type. For consistency with the previous sentence, it might be better to write this as "which represents an exclusive-or" relationship.In logic/circuits, this is often written as
XOR
, which we could potentially use, but I think that exclusive-or is more straightforward.