Skip to content

Instantly share code, notes, and snippets.

@bloderxd
Created March 17, 2023 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bloderxd/2e4572699344f196f014b6a9e85cc9b0 to your computer and use it in GitHub Desktop.
Save bloderxd/2e4572699344f196f014b6a9e85cc9b0 to your computer and use it in GitHub Desktop.
typealias HomList<A, B> = ((A) -> B) -> List<B>
fun <A, B> List<A>.yoneda(): HomList<A, B> {
return { f -> this.map(f) }
}
fun <A, B> HomList<A, B>.lower(): List<A> {
return buildList {
this@lower { a ->
add(a)
Unit as B
}
}
}
fun main() {
val numbers: List<Int> = listOf(1, 2, 3)
val yoneda = numbers.yoneda<_, Int>()
val yonedaString = numbers.yoneda<_, String>()
println(yoneda { it + 1 }) // [2, 3, 4]
println(yonedaString { "Number: $it" }) // [Number: 1, Number: 2, Number: 3]
println(yoneda.lower()) // [1, 2, 3]
println(yonedaString.lower()) // [1, 2, 3]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment