Created
March 17, 2023 21:15
-
-
Save bloderxd/2e4572699344f196f014b6a9e85cc9b0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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