Skip to content

Instantly share code, notes, and snippets.

@happy-bracket
Created August 22, 2019 05:39
Show Gist options
  • Save happy-bracket/0d5493fce865ebb113bf2e20b9fd6198 to your computer and use it in GitHub Desktop.
Save happy-bracket/0d5493fce865ebb113bf2e20b9fd6198 to your computer and use it in GitHub Desktop.
import io.reactivex.Single
import io.reactivex.functions.BiFunction
interface Kind<out C, out A>
class ForSingle private constructor()
class SingleContainer<T>(val single: Single<T>): Kind<ForSingle, T>
fun <T> Kind<ForSingle, T>.fix() = (this as SingleContainer<T>).single
fun <T> Single<T>.unfix() = SingleContainer(this)
interface Math<F> {
fun lift(value: Int): Kind<F, Int>
fun add(value1: Kind<F, Int>, value2: Kind<F, Int>): Kind<F, Int>
}
class RxMath : Math<ForSingle> {
override fun lift(value: Int): Kind<ForSingle, Int> = Single.just(value).unfix()
override fun add(value1: Kind<ForSingle, Int>, value2: Kind<ForSingle, Int>): Kind<ForSingle, Int> =
value1.fix().zipWith<Int, Int>(value2.fix(), BiFunction(Int::plus))
.unfix()
}
interface Bridge<F, A> {
fun apply(math: Math<F>): Kind<F, A>
}
fun RxLift(value: Int) =
object : Bridge<ForSingle, Int> {
override fun apply(math: Math<ForSingle>): Kind<ForSingle, Int> {
return math.lift(value)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment