-
Notifications
You must be signed in to change notification settings - Fork 450
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
MonadFilter docs and comprehension guards #441
Conversation
Codecov Report
@@ Coverage Diff @@
## master #441 +/- ##
============================================
- Coverage 36.29% 36.25% -0.04%
- Complexity 312 320 +8
============================================
Files 173 174 +1
Lines 4794 4821 +27
Branches 515 523 +8
============================================
+ Hits 1740 1748 +8
- Misses 2910 2925 +15
- Partials 144 148 +4
Continue to review full report at Codecov.
|
open class MonadFilterContinuation<F, A>(val MF: MonadFilter<F>, override val context: CoroutineContext = EmptyCoroutineContext) : | ||
MonadContinuation<F, A>(MF) { | ||
|
||
object PredicateInterrupted : RuntimeException() |
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.
Even if this is just for internal use, please add an error description. I'd also make the object internal
or even private
.
Law("MonadFilter Laws: Right Empty", { monadFilterRightEmpty(MF, cf, EQ) }), | ||
Law("MonadFilter Laws: Consistency", { monadFilterConsistency(MF, cf, EQ) })) | ||
Law("MonadFilter Laws: Right Empty", { monadFilterRightEmpty(MF, EQ) }), | ||
Law("MonadFilter Laws: Consistency", { monadFilterConsistency(MF, cf, EQ) }), |
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.
Can we use applicative pure instead of constructor function?
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 just removed that cf
param because it was not used but did not added anything to that in this PR. But presumably yes.
@@ -5,3 +5,103 @@ permalink: /docs/typeclasses/monadfilter/ | |||
--- | |||
|
|||
## MonadFilter | |||
|
|||
`MonadFilter` is a type class that abstract away the option of interrupting computation if a given predicate is 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.
typo: that abstracts
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.
LGTM, just a couple of nits
I meant a description message in RuntimeException, not on the documentation, but close enough hahahaha |
This PR Fixes #336 and enables
continueIf
andbindWithFilter
on monad comprehensions for instances adhering toMonadFilter
which can provide an emptyF<A>
for a given Monad.Docs with examples shown below
MonadFilter
MonadFilter
is a type class that abstract away the option of interrupting computation if a given predicate is not satisfied.All instances of
MonadFilter
provide syntax over their respective data types to comprehend monadically over their computation:continueWith
Binding over
MonadFilter
instances withbindingFilter
brings into scope thecontinueIf
guard that requires aBoolean
predicate as value. If the predicate istrue
the computation will continue and if the predicate returnsfalse
the computation is short-circuited returning monad filter instanceempty()
value.In the example below we demonstrate monadic comprehension over the
MonadFilter
instances for bothOption
andListKW
since both data types can provide a safeempty
value.When
continueIf
is satisfied the computation continuesWhen
continueIf
returnsfalse
the computation is interrupted and theempty()
value is returnedbindWithFilter
Binding over
MonadFilter
instances withbindingFilter
brings into scope thebindWithFilter
guard that requires aBoolean
predicate as value getting matched on the monad capturing inner value. If the predicate istrue
the computation will continue and if the predicate returnsfalse
the computation is short-circuited returning the monad filter instanceempty()
value.When
bindWithFilter
is satisfied the computation continuesWhen
bindWithFilter
returnsfalse
the computation short circuits yielding the monad's empty value