-
Notifications
You must be signed in to change notification settings - Fork 454
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9733a64
MonadFilter comprehensions guards with `continueIf`
raulraja 32ad675
added some docs for MonadFilter
raulraja b107337
MonadFilter comprehensions guards with `bindWithFilter`
raulraja 65a42ba
detekt fixes
raulraja 670a589
doc fixes
raulraja 7584d54
Merge branch 'master' into rr-monad-filter-comprehensions-guards
raulraja d880492
Make PredicateInterrupted private and added description
raulraja 4358f6e
s/abstract/abstracts
raulraja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
kategory-core/src/main/kotlin/kategory/typeclasses/MonadFilterContinuation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package kategory | ||
|
||
import kotlin.coroutines.experimental.CoroutineContext | ||
import kotlin.coroutines.experimental.EmptyCoroutineContext | ||
import kotlin.coroutines.experimental.RestrictsSuspension | ||
|
||
@RestrictsSuspension | ||
open class MonadFilterContinuation<F, A>(val MF: MonadFilter<F>, override val context: CoroutineContext = EmptyCoroutineContext) : | ||
MonadContinuation<F, A>(MF) { | ||
|
||
/** | ||
* marker exception that interrupts the coroutine flow and gets captured | ||
* to provide the monad empty value | ||
*/ | ||
private object PredicateInterrupted : RuntimeException() | ||
|
||
override fun resumeWithException(exception: Throwable) { | ||
when (exception) { | ||
is PredicateInterrupted -> returnedMonad = MF.empty() | ||
else -> super.resumeWithException(exception) | ||
} | ||
} | ||
|
||
/** | ||
* Short circuits monadic bind if `predicate == false` return the | ||
* monad `empty` value. | ||
*/ | ||
fun continueIf(predicate: Boolean): Unit { | ||
if (!predicate) throw PredicateInterrupted | ||
} | ||
|
||
/** | ||
* Binds only if the given predicate matches the inner value otherwise binds into the Monad `empty()` value | ||
* on `MonadFilter` instances | ||
*/ | ||
suspend fun <B> HK<F, B>.bindWithFilter(f: (B) -> Boolean): B { | ||
val b: B = bind { this } | ||
return if (f(b)) b else bind { MF.empty<B>() } | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.