-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #137 from kategory/paco-function1
Add Function1
- Loading branch information
Showing
7 changed files
with
110 additions
and
10 deletions.
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
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,32 @@ | ||
package kategory | ||
|
||
typealias Function1F<R> = HK<Function1.F, R> | ||
|
||
fun <P, R> ((P) -> R).k(): Function1<P, R> = | ||
Function1(this) | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
fun <R> Function1F<R>.ev(): FunctionInject<R> = | ||
this as FunctionInject<R> | ||
|
||
// We don't want an inherited class to avoid equivalence issues, so a simple HK wrapper will do | ||
data class Function1<in A, out R>(val f: (A) -> R) : FunctionInject<R>, Function1F<R> { | ||
override fun <P> invokeInject(p: P): R = | ||
f(p as A) | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
operator fun invoke(a: A): R = | ||
f(a) | ||
|
||
class F private constructor() | ||
|
||
companion object { | ||
fun <P> functor() = object : Function1Instances<P> {} | ||
|
||
fun <P> applicative() = object : Function1Instances<P> {} | ||
|
||
fun <P> monad() = object : Function1Instances<P> {} | ||
|
||
fun <P> monadReader() = object : Function1Instances<P> {} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
kategory/src/main/kotlin/kategory/arrow/FunctionBijections.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,9 @@ | ||
package kategory | ||
|
||
interface FunctionInject<out R> { | ||
fun <P> invokeInject(p: P): R | ||
} | ||
|
||
interface FunctionSurject<in P> { | ||
fun <R> invokeSurject(p: P): R | ||
} |
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
31 changes: 31 additions & 0 deletions
31
kategory/src/main/kotlin/kategory/instances/Function1Instances.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,31 @@ | ||
package kategory | ||
|
||
interface Function1Instances<P> : | ||
Functor<Function1.F>, | ||
Applicative<Function1.F>, | ||
Monad<Function1.F>, | ||
MonadReader<Function1.F, P> { | ||
|
||
override fun ask(): HK<Function1.F, P> = | ||
{ a: P -> a }.k() | ||
|
||
override fun <A> local(f: (P) -> P, fa: HK<Function1.F, A>): Function1<P, A> = | ||
f.andThen { fa.ev().invokeInject(it) }.k() | ||
|
||
override fun <A> pure(a: A): Function1<P, A> = | ||
{ _: P -> a }.k() | ||
|
||
override fun <A, B> map(fa: HK<Function1.F, A>, f: (A) -> B): HK<Function1.F, B> = | ||
f.compose { b: B -> fa.ev().invokeInject(b) }.k() | ||
|
||
override fun <A, B> flatMap(fa: HK<Function1.F, A>, f: (A) -> HK<Function1.F, B>): Function1<P, B> = | ||
Function1 { p -> f(fa.ev().invokeInject(p)).ev().invokeInject(p) } | ||
|
||
override fun <A, B> tailRecM(a: A, f: (A) -> HK<Function1.F, Either<A, B>>): Function1<P, B> = | ||
Function1 { p -> | ||
tailrec fun loop(thisA: A): B = | ||
f(thisA).ev().invokeInject(p).fold({ loop(it) }, { it }) | ||
|
||
loop(a) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package kategory | ||
|
||
import io.kotlintest.KTestJUnitRunner | ||
import io.kotlintest.matchers.shouldBe | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(KTestJUnitRunner::class) | ||
class Function1Test : UnitSpec() { | ||
init { | ||
testLaws(MonadLaws.laws(Function1.monad<Int>(), object : Eq<HK<Function1.F, Int>> { | ||
override fun eqv(a: HK<Function1.F, Int>, b: HK<Function1.F, Int>): Boolean = | ||
a.ev().invokeInject(1) == b.ev().invokeInject(1) | ||
})) | ||
|
||
"bla" { | ||
{ a: Int -> a + 1 }.k().invoke(1) | ||
} | ||
|
||
"Function1Monad.binding should for comprehend over all values of multiple Function0" { | ||
val M = Function1.monad<Int>() | ||
M.binding { | ||
val x = Function1 { _: Int -> 1 }.bind() | ||
val y = !Function1 { _: Int -> 2 } | ||
val z = bind { Function1 { _: Int -> 3 } } | ||
yields(x + y + z) | ||
}.ev().invokeInject(0) shouldBe 6 | ||
} | ||
} | ||
} |