Files
kotlin-fork/compiler/testData/ir/irText/declarations/contextReceivers/fromKEEP/monoidSum.kt
T
Sergej Jaskiewicz f2031ae642 [IR] Don't print multifile/synthetic facade class names in irText tests
This only applies to JVM and fq-names in declaration references
in IR dumps.

This enables us to run more irText tests on platforms other than JVM
(see KT-58605).
2023-06-05 10:40:17 +00:00

30 lines
677 B
Kotlin
Vendored

// !LANGUAGE: +ContextReceivers
// WITH_STDLIB
interface Semigroup<T> {
infix fun T.combine(other: T): T
}
interface Monoid<T> : Semigroup<T> {
val unit: T
}
object IntMonoid : Monoid<Int> {
override fun Int.combine(other: Int): Int = this + other
override val unit: Int = 0
}
object StringMonoid : Monoid<String> {
override fun String.combine(other: String): String = this + other
override val unit: String = ""
}
context(Monoid<T>)
fun <T> List<T>.sum(): T = fold(unit) { acc, e -> acc.combine(e) }
fun box(): String {
with(IntMonoid) {
listOf(1, 2, 3).sum()
}
return with(StringMonoid) {
listOf("O", "K").sum()
}
}