Skip to content
This repository has been archived by the owner on Feb 24, 2021. It is now read-only.

Commit

Permalink
Fixes NPE in bindWithFilter (#134)
Browse files Browse the repository at this point in the history
* Change MonadFilterLaws to use a gen to test bindWithFilter in order to generate all possible values

* Fix bindWithFilter by filtering first, then binding (thanks to @nomisRev)

Co-authored-by: danieh <daniel.montoya@47deg.com>
  • Loading branch information
aballano and danimontoya authored May 29, 2020
1 parent e9d2350 commit 5ae4cfa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ interface MonadFilterSyntax<F> : MonadSyntax<F> {
suspend fun <B> Kind<F, B>.bindWithFilter(f: (B) -> Boolean): B
}

open class MonadFilterContinuation<F, A>(val MF: MonadFilter<F>, override val context: CoroutineContext = EmptyCoroutineContext) :
MonadContinuation<F, A>(MF), MonadFilterSyntax<F> {
open class MonadFilterContinuation<F, A>(
val MF: MonadFilter<F>,
override val context: CoroutineContext = EmptyCoroutineContext
) : MonadContinuation<F, A>(MF), MonadFilterSyntax<F> {

override fun resumeWith(result: Result<Kind<F, A>>) {
result.fold({ super.resumeWith(result) }, {
Expand All @@ -43,8 +45,8 @@ open class MonadFilterContinuation<F, A>(val MF: MonadFilter<F>, override val co
* Binds only if the given predicate matches the inner value otherwise binds into the Monad `empty()` value
* on `MonadFilter` instances
*/
override suspend fun <B> Kind<F, B>.bindWithFilter(f: (B) -> Boolean): B {
val b: B = this.bind()
return if (f(b)) b else MF.empty<B>().bind()
}
override suspend fun <B> Kind<F, B>.bindWithFilter(f: (B) -> Boolean): B =
MF.run {
this@bindWithFilter.filter(f)
}.bind()
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ object MonadFilterLaws {
Law("MonadFilter Laws: Right Empty") { MF.monadFilterRightEmpty(GEN, EQ) },
Law("MonadFilter Laws: Consistency") { MF.monadFilterConsistency(GEN, EQ) },
Law("MonadFilter Laws: Comprehension Guards") { MF.monadFilterEmptyComprehensions(EQ) },
Law("MonadFilter Laws: Comprehension bindWithFilter Guards") { MF.monadFilterBindWithFilterComprehensions(EQ) })
Law("MonadFilter Laws: Comprehension bindWithFilter Guards") { MF.monadFilterBindWithFilterComprehensions(GEN, EQ) })
}

fun <F> laws(
Expand Down Expand Up @@ -76,11 +76,11 @@ object MonadFilterLaws {
}.equalUnderTheLaw(if (!guard) empty() else just(n), EQ)
}

fun <F> MonadFilter<F>.monadFilterBindWithFilterComprehensions(EQ: Eq<Kind<F, Int>>): Unit =
forAll(Gen.bool(), Gen.int()) { guard: Boolean, n: Int ->
fun <F, A> MonadFilter<F>.monadFilterBindWithFilterComprehensions(G: Gen<Kind<F, A>>, EQ: Eq<Kind<F, A>>): Unit =
forAll(Gen.bool(), G) { guard: Boolean, fa: Kind<F, A> ->
fx.monadFilter {
val x = just(n).bindWithFilter { _ -> guard }
val x = fa.bindWithFilter { guard }
x
}.equalUnderTheLaw(if (!guard) empty() else just(n), EQ)
}.equalUnderTheLaw(if (!guard) empty() else fa, EQ)
}
}

0 comments on commit 5ae4cfa

Please sign in to comment.