Add ifTrueM, ifFalseM, ensureTrue and ensureFalse#4637
Add ifTrueM, ifFalseM, ensureTrue and ensureFalse#4637geirolz wants to merge 4 commits intotypelevel:mainfrom
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
| } | ||
|
|
||
| /** | ||
| * If the `F[Boolean]` is `true` then return `ifTrue` otherwise return `ifFalse` |
There was a problem hiding this comment.
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.
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
ifTrueMandifFalseMforMonadandensureTrueandensureFalseforMonadError.The reasons why they are under
Monadand notApplicativeis because they needspurefor the unhandled branch.Considerations
ifTrueM/ifFalseM- Idk if this should stay inMonador 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!