-
-
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
MonadError instance for Ior #1548
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1548 +/- ##
==========================================
+ Coverage 92.42% 93.09% +0.67%
==========================================
Files 249 250 +1
Lines 3971 3983 +12
Branches 146 130 -16
==========================================
+ Hits 3670 3708 +38
+ Misses 301 275 -26
Continue to review full report at Codecov.
|
implicit def catsDataMonadErrorForIor[A: Semigroup]: MonadError[Ior[A, ?], A] with Traverse[Ior[A, ?]] = | ||
new MonadError[Ior[A, ?], A] with Traverse[Ior[A, ?]] { | ||
|
||
def raiseError[B](e: A): Ior[A, B] = Ior.left(e) |
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.
This line is not tested
|
||
def handleErrorWith[B](fa: Ior[A, B])(f: (A) => Ior[A, B]): Ior[A, B] = | ||
fa match { | ||
case Ior.Left(e) => f(e) |
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.
This line is not tested
fa match { | ||
case Ior.Left(e) => f(e) | ||
case r @ Ior.Right(_) => r | ||
case Ior.Both(e, _) => f(e) |
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.
This line is not tested
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.
@kailuowang Thanks for pointing them out. I will get it fixed this week.
|
||
implicit def catsDataBifunctorForIor: Bifunctor[Ior] = | ||
new Bifunctor[Ior] { | ||
override def bimap[A, B, C, D](fab: A Ior B)(f: A => C, g: B => D): C Ior D = fab.bimap(f, g) | ||
} | ||
} | ||
|
||
private[data] sealed abstract class IorInstances0 { | ||
|
||
implicit def catsDataTraverseFunctorForIor[A]: Traverse[A Ior ?] with Functor[A Ior ?] = new Traverse[A Ior ?] with Functor[A Ior ?] { |
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 why you decided to merge this into the MonadError
instance, but this one does not require A
having a Semigroup
instance. So if we don't have good reason, we should probably keep as is.
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.
@kailuowang wouldn't I need it when I define a type like this:
type IorNel[A, B] = Ior[NonEmptyList[A], B]
If I try to use the traverse instance? I saw the mixin of Traverse and ApplicativeError on Validated so I thought it would be equivalent, or in this case is not relevant? I am considering this since I already have a PR #1540 with a proposition on that IorNel.
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.
+1 to keep. Traverse should not require a Semigroup.
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.
@leandrob13 for a IorNel
whether to require the left side being a Semigroup
is irrelevant because the left side NonEmptyList
is one. But there are other types of Ior
in which the left side is not a Semigroup
. We don't want to add unnecessary requirements/restriction to our instances.
I think the mixin in Validated
is not optimal either in the exact same sense. The Traverse
and Functor
shouldn't need a Semigroup
. Care to fix that one as well?
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.
@kailuowang understood! I will fix the Validated instance as well. Thanks for the feedback.
i.valueOr(f) should === (i.leftMap(f).fold(identity, identity, _ + _)) | ||
} | ||
} | ||
|
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 for adding coverage.
👍 LKTM, thanks very much @leandrob13 for all the efforts to address the feedbacks! |
@kailuowang you are welcome. I have learned a lot. |
@@ -357,28 +388,6 @@ private[data] sealed abstract class ValidatedInstances extends ValidatedInstance | |||
|
|||
override def isEmpty[A](fa: Validated[E, A]): Boolean = fa.isInvalid | |||
} | |||
// scalastyle:on method.length |
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.
We may want to keep this // scalastyle:off method.length
comment.
At the same time I think we can remove these comments around the ApplicativeError
instance (catsDataApplicativeErrorForValidated
).
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.
@peterneyens I'm on it!
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.
@peterneyens done with requested changes.
import cats.kernel.laws.GroupLaws | ||
import cats.laws.discipline.{BifunctorTests, CartesianTests, MonadErrorTests, SerializableTests, TraverseTests} | ||
import cats.data.{Ior, NonEmptyList, EitherT} | ||
import cats.kernel.laws.GroupLaws |
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.
You had this import already on line 4 it seems.
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.
@peterneyens darn it. That one must have slipped when I merged. I'm on it.
Fixes #1544 and #1553