Skip to content
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 Bimonad for Function0 #95

Merged
merged 7 commits into from
May 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions katz/src/main/kotlin/katz/instances/Function0Bimonad.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package katz

// We don't we want an inherited class to avoid equivalence issues, so a simple HK wrapper will do
data class Function0<out A>(internal val f: () -> A) : HK<Function0.F, A> {

class F private constructor()

companion object : Bimonad<Function0.F>, GlobalInstance<Bimonad<Function0.F>>() {

override fun <A, B> flatMap(fa: HK<Function0.F, A>, f: (A) -> HK<Function0.F, B>): HK<Function0.F, B> =
f(fa.ev().invoke())

override fun <A, B> coflatMap(fa: HK<Function0.F, A>, f: (HK<Function0.F, A>) -> B): HK<Function0.F, B> =
{ f(fa) }.k()

override fun <A> pure(a: A): HK<Function0.F, A> =
{ a }.k()

override fun <A> extract(fa: HK<Function0.F, A>): A =
fa.ev().invoke()

override fun <A, B> tailRecM(a: A, f: (A) -> HK<F, Either<A, B>>): HK<F, B> =
f(a).ev().invoke().let { either ->
when (either) {
is Either.Left -> tailRecM(either.a, f)
is Either.Right -> ({ either.b }).k()
}
}
}
}

fun <A> (() -> A).k(): HK<Function0.F, A> = Function0(this)

fun <A> HK<Function0.F, A>.ev() = (this as Function0<A>).f
3 changes: 1 addition & 2 deletions katz/src/test/kotlin/katz/data/CoreaderTest.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package katz.data
package katz

import io.kotlintest.KTestJUnitRunner
import io.kotlintest.matchers.shouldBe
import io.kotlintest.properties.forAll
import katz.*
import org.junit.runner.RunWith

@RunWith(KTestJUnitRunner::class)
Expand Down
33 changes: 33 additions & 0 deletions katz/src/test/kotlin/katz/data/Function0Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package katz

import io.kotlintest.KTestJUnitRunner
import io.kotlintest.matchers.shouldBe
import org.junit.runner.RunWith

@RunWith(KTestJUnitRunner::class)
class Function0Test : UnitSpec() {
init {
"Function0Monad.binding should for comprehend over all values of multiple Function0" {
Function0.binding {
val x = Function0 { 1 }.bind()
val y = !Function0 { 2 }
val z = bind { Function0 { 3 } }
yields(x + y + z)
}.ev().invoke() shouldBe 6
}

"Function0Comonad.cobinding should for comprehend over all values of multiple Function0" {
Function0.cobinding {
val x = Function0 { 1 }.extract()
val y = !Function0 { 2 }
val z = extract { Function0 { 3 } }
yields(x + y + z)
} shouldBe 6
}

"Function0Comonad.duplicate should create an instance of Function0<Function0<A>>" {
Function0.duplicate(Function0 { 3 }).ev().invoke().ev().invoke() shouldBe
Function0 { Function0 { 3 } }.ev().invoke().ev().invoke()
}
}
}
6 changes: 1 addition & 5 deletions katz/src/test/kotlin/katz/data/IdTest.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package katz.data
package katz

import io.kotlintest.KTestJUnitRunner
import io.kotlintest.matchers.shouldBe
import katz.Id
import katz.UnitSpec
import katz.binding
import katz.cobinding
import org.junit.runner.RunWith

@RunWith(KTestJUnitRunner::class)
Expand Down
3 changes: 1 addition & 2 deletions katz/src/test/kotlin/katz/data/ReaderTest.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package katz.data
package katz

import io.kotlintest.KTestJUnitRunner
import io.kotlintest.matchers.shouldBe
import katz.*
import org.junit.runner.RunWith

@RunWith(KTestJUnitRunner::class)
Expand Down