-
-
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 ensureWith to Validated and Either (#1550) #1612
Conversation
I think additional tests would be nice for these logic. |
Codecov Report
@@ Coverage Diff @@
## master #1612 +/- ##
==========================================
+ Coverage 93.38% 93.43% +0.04%
==========================================
Files 241 241
Lines 4054 4068 +14
Branches 134 138 +4
==========================================
+ Hits 3786 3801 +15
+ Misses 268 267 -1
Continue to review full report at Codecov.
|
It looks like ScalaDoc isn't happy about the |
Thank you @ceedubs! CI seems to be happy now :) |
} | ||
} | ||
|
||
test("ensureWith should fail if predicate not satisfied") { |
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.
Should we add this test to EitherTests
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.
Done!
Thanks @LukaJCB! This LGTM. The only thing that I'm not sure about is the name It doesn't necessarily need to be done in this PR, but I think that |
Thanks for your feedback! :) |
Given that we are close to 1.0.0-MF, I kind of prefer having the addition to |
This is going to be really useful. I constantly need this feature.
def monadErrorEnsureConsistency[A](fa: F[A], e: E, p: A => Boolean): IsEq[F[A]] =
F.ensure(fa)(e)(p) <-> F.flatMap(fa)(a => if (p(a)) F.pure(a) else F.raiseError(e))
def monadErrorEnsureOrConsistency[A](fa: F[A], e: A => E, p: A => Boolean): IsEq[F[A]] =
F.ensure(fa)(e)(p) <-> F.flatMap(fa)(a => if (p(a)) F.pure(a) else F.raiseError(e(a)))
|
So implemented the changes, but removing the tests seems to have reduced the coverage, will investigate soon :D |
The newly added laws need to go into the tests in the discipline package. |
Oops, yeah, that should've been obvious to me, fixed! |
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! 👍 LGTM
@@ -63,6 +63,11 @@ final class EitherOps[A, B](val eab: Either[A, B]) extends AnyVal { | |||
case Right(b) => if (f(b)) eab else Left(onFailure) | |||
} | |||
|
|||
def ensureOr[AA >: A](onFailure: B => AA)(f: B => Boolean): Either[AA, B] = eab match { |
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.
It looks like this is following a precedent, but I find myself wondering if this Either
syntax should really be providing syntax for methods that should already be available via type class syntax (in this case MonadError
). Let's table that for a separate issue though.
thanks so much @LukaJCB , sorry about the delay. LGTM. 👍 |
There seem to be some merge conflicts, otherwise this looks OK 👍 , thanks @LukaJCB ! I wonder if we maybe should implement |
I took the liberty of resolving the conflict using github's merging too. Will merge if build green. |
Resolves #1550. Also looking for feedback on naming here, or maybe I should be adding an additional test apart from the doc test. Would be much appreciated :)