-
-
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
Merged
Merged
Add docs for Ior #1822
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
--- | ||
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 "exclusive-or" 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._ | ||
|
||
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] | ||
|
||
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.one("Can't be empty").leftIor | ||
else if (u.contains(".")) | ||
Ior.both(Nel.one("Dot in name is deprecated"), Username(u)) | ||
else | ||
Username(u).rightIor | ||
} | ||
|
||
def validatePassword(p: String): Failures Ior Password = { | ||
if (p.length < 8) | ||
Nel.one("Password too short").leftIor | ||
else if (p.length < 10) | ||
Ior.both(Nel.one("Password should be longer"), Password(p)) | ||
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}" | ||
) | ||
|
||
``` | ||
Similar to [Validated](validated.html), there is also a type alias for using a `NonEmptyList` on the left side. | ||
|
||
```tut:silent | ||
type IorNel[B, A] = Ior[NonEmptyList[B], A] | ||
``` | ||
|
||
|
||
```tut | ||
|
||
val left: IorNel[String, Int] = Ior.leftNel("Error") | ||
|
||
``` | ||
|
||
|
||
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 | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Not sure if we want to introduce that here, but there also is a
IorNel
type alias.