-
-
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 ifTrueM, ifFalseM, ensureTrue and ensureFalse #4637
base: main
Are you sure you want to change the base?
Add ifTrueM, ifFalseM, ensureTrue and ensureFalse #4637
Conversation
I'm concerned with Monoid being used just to get a default value and not due to any laws about monoid relating to the functions using it. |
I see you point - I don't have strong options here. The alternative is to return |
core/src/main/scala/cats/Monad.scala
Outdated
@@ -162,6 +164,18 @@ trait Monad[F[_]] extends FlatMap[F] with Applicative[F] { | |||
tailRecM(branches.toList)(step) | |||
} | |||
|
|||
/** | |||
* If the `F[Boolean]` is `true` then return `ifTrue` otherwise return `ifFalse` |
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.
The doc doesn't match with the implementation
Just for reference, to keep stuff connected: #4626 I also think that instead of |
/** | ||
* If the `F[Boolean]` is `true` then return `ifTrue` otherwise return `Monoid[A].empty` | ||
*/ | ||
def ifTrueM[B: Monoid](fa: F[Boolean])(ifTrue: => F[B]): F[B] = | ||
ifM(fa)(ifTrue, pure(Monoid[B].empty)) | ||
|
||
/** | ||
* If the `F[Boolean]` is `false` then return `ifFalse` otherwise return `Monoid[A].empty` | ||
*/ | ||
def ifFalseM[B: Monoid](fa: F[Boolean])(ifFalse: => F[B]): F[B] = | ||
ifM(fa)(pure(Monoid[B].empty), ifFalse) |
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'd suggest adding these methods to the F[Boolean]
syntax in Mouse https://github.com/typelevel/mouse/blob/main/shared/src/main/scala/mouse/fboolean.scala
This PR adds
ifTrueM
andifFalseM
forMonad
andensureTrue
andensureFalse
forMonadError
.The reasons why they are under
Monad
and notApplicative
is because they needspure
for the unhandled branch.Considerations
ifTrueM
/ifFalseM
- Idk if this should stay inMonad
or just in the syntax since depends onMonoid
.ensureTrue
/ensureFalse
- I was unsure about the naming between these andraiseWhenTrue
/raiseWhenFalse
.Let me know what do you think!