Skip to content

Instantly share code, notes, and snippets.

View drawers's full-sized avatar
🟩
In greenish twilight at the bottom of the Rhine

David Rawson drawers

🟩
In greenish twilight at the bottom of the Rhine
View GitHub Profile
@drawers
drawers / HigherKinds.kt
Last active January 10, 2021 23:36
Higher kinds tl;dr
// I want to parameterize a container F as well as the contents A
Mappable<F<A>>
// but type parameters can't have type parameters in Kotlin :-(
// Instead of a type parameter of a type parameter,
// what if I just use two type parameters?
Mappable<Kind<F,A>>
fun <T> consumeListOrSequence(mappable: Mappable<T>) {
mappable.map { it }
}
fun SingleK.Companion.runtime(ctx: RuntimeContext) = object : Runtime<ForSingleK>(
SingleK.concurrent(
object : Dispatchers<ForSingleK> {
override fun default(): CoroutineContext = Schedulers.computation().asCoroutineContext()
override fun io(): CoroutineContext = Schedulers.io().asCoroutineContext()
}
), ctx
) {}
fun IO.Companion.runtime(ctx: RuntimeContext) = object : Runtime<ForIO>(
IO.concurrent(), ctx
) {}
@drawers
drawers / News.kt
Last active November 15, 2020 20:05
fun <F> Runtime<F>.loadNews(): Kind<F, List<NewsItem>>
@drawers
drawers / Option.kt
Last active December 14, 2020 21:39
@higherkind
sealed class Option<out A> : OptionOf<A> {
// snip
}
// the annotation processor will emit the following code for the witness:
// class ForOption private constructor() { companion object }
// typealias OptionOf<A> = arrow.Kind<ForOption, A>
@extension
interface OptionMappable : Mappable<ForOption> {
override fun <A, B> OptionOf<A>.map(f: (A) -> B): Option<B> =
fix().map(f)
}
interface Mappable<F> {
fun <A, B> Kind<F, A>.map(f: (A) -> B): Kind<F, B>
}
class ForOption private constructor() { companion object }
typealias OptionOf<A> = arrow.Kind<ForOption, A>
@Suppress("UNCHECKED_CAST", "NOTHING_TO_INLINE")
inline fun <A> OptionOf<A>.fix(): Option<A> =
this as Option<A>
@drawers
drawers / Fix.kt
Last active January 11, 2021 02:20
val a: Kind<ForOption, Int> = Option(1)
val fixedA: Option<Int> = a.fix()