-
-
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
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1822 +/- ##
=======================================
Coverage 95.54% 95.54%
=======================================
Files 248 248
Lines 4420 4420
Branches 121 124 +3
=======================================
Hits 4223 4223
Misses 197 197 Continue to review full report at Codecov.
|
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.
noice, thanks!
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.
Thanks @LukaJCB. Good stuff!
I left one small comment about an Xor
reference that I think it might be a good idea for us to change.
My favorite example of Ior
is using List[A Ior B]
as a return type for a method that zips two lists together, but I suppose it would be out of place to shove that example into this doc :)
docs/src/main/tut/datatypes/ior.md
Outdated
# 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. |
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 an Xor
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.
docs/src/main/tut/datatypes/ior.md
Outdated
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Without the Ior
syntax, you could do Ior.leftNel("Can't be empty")
.
docs/src/main/tut/datatypes/ior.md
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
If you are creating a one element NonEmptyList
you can use NonEmptyList.one
instead of NonEmptyList.of
.
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 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.
Something which would be nice as well is to show the difference between the Something like : import cats.data.{ IorNel, NonEmptyList, ValidatedNel }
import cats.implicits._
val oddOrEvenEither: Int => Either[Int, Int] =
n => Either.cond(n % 2 == 0, n, n)
val oddOrEvenValidatedNel: Int => ValidatedNel[Int, Int] =
oddOrEven(_).toValidated.toValidatedNel
val oddOrEvenIorNel: Int => IorNel[Int, Int] =
oddOrEven(_).toIor.leftMap(NonEmptyList.one)
type OddEither[A] = Either[Int, A]
type OddNelVal[A] = ValidatedNel[Int, A]
type OddNelIor[A] = IorNel[Int, A]
val nel = NonEmptyList.of(1, 2, 3, 4, 5)
// Ior short circuits like Either
nel.traverse[OddEither, Int](oddOrEvenEither) // Left(1)
nel.traverse[OddNelVal, Int](oddOrEvenValidatedNel) // Invalid(NonEmptyList(1, List(3, 5)))
nel.traverse[OddNelIor, Int](oddOrEvenIorNel) // Left(NonEmptyList(1, List()))
// for Either and Validated, Semigroup same result as with applicative
// but for Ior gives both sides
nel.reduceMap(oddOrEvenEither) // Left(1)
nel.reduceMap(oddOrEvenValidatedNel) // Invalid(NonEmptyList(1, List(3, 5)))
nel.reduceMap(oddOrEvenIorNel) // Both(NonEmptyList(1, List(3, 5)), 6) I can add this in a follow up PR if needed. |
e094321
to
01aa2f0
Compare
LGTM. @peterneyens are there any pending items you wanted to see make it into this before merging? |
No, I'll follow up with another PR. |
Some basic usage so far. Might make sense to also document more of the methods available on
Ior
:)