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 Alternative #256

Merged
merged 6 commits into from
Sep 5, 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
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ interface StateTSemigroupK<F, S> : SemigroupK<StateTKindPartial<F, S>> {
override fun <A> combineK(x: HK<HK2<StateTHK, F, S>, A>, y: HK<HK2<StateTHK, F, S>, A>): StateT<F, S, A> =
StateT(F(), F().pure({ s -> G().combineK(x.ev().run(s), y.ev().run(s)) }))
}

7 changes: 7 additions & 0 deletions kategory/src/main/kotlin/kategory/typeclasses/Alternative.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package kategory.typeclasses

import kategory.Applicative
import kategory.MonoidK
import kategory.Typeclass

interface Alternative<F> : Applicative<F>, MonoidK<F>, Typeclass
18 changes: 18 additions & 0 deletions kategory/src/main/kotlin/kategory/typeclasses/Composed.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package kategory

import kategory.typeclasses.Alternative

/**
* https://www.youtube.com/watch?v=wvSP5qYiz4Y
*/
Expand Down Expand Up @@ -177,6 +179,22 @@ interface ComposedApplicative<F, G> : Applicative<ComposedType<F, G>>, ComposedF

inline fun <reified F, reified G> Applicative<F>.compose(GA: Applicative<G> = applicative<G>()): Applicative<ComposedType<F, G>> = ComposedApplicative(this, GA)

interface ComposedAlternative<F, G> : Alternative<ComposedType<F, G>>, ComposedApplicative<F, G>, ComposedMonoidK<F, G> {
override fun F(): Alternative<F>

companion object {
operator fun <F, G> invoke(AF: Alternative<F>, AG: Applicative<G>)
: Alternative<ComposedType<F, G>> =
object : ComposedAlternative<F, G> {
override fun F(): Alternative<F> = AF

override fun G(): Applicative<G> = AG
}
}
}

inline fun <reified F, reified G> Alternative<F>.compose(GA: Applicative<G> = applicative<G>()): Alternative<ComposedType<F, G>> = ComposedAlternative(this, GA)

interface ComposedFunctorFilter<F, G> : FunctorFilter<ComposedType<F, G>>, ComposedFunctor<F, G> {

override fun F(): Functor<F>
Expand Down