Files
kotlin-fork/compiler/testData/ir/irText/declarations/contextReceivers/fromKEEP/monoidSum.fir.kt.txt
T
2024-02-16 10:19:38 +00:00

64 lines
1.4 KiB
Kotlin
Vendored

interface Monoid<T : Any?> : Semigroup<T> {
abstract val unit: T
abstract get
}
interface Semigroup<T : Any?> {
abstract infix fun T.combine(other: T): T
}
object IntMonoid : Monoid<Int> {
override val unit: Int
field = 0
override get
private constructor() /* primary */ {
super/*Any*/()
/* <init>() */
}
override infix fun Int.combine(other: Int): Int {
return <this>.plus(other = other)
}
}
object StringMonoid : Monoid<String> {
override val unit: String
field = ""
override get
private constructor() /* primary */ {
super/*Any*/()
/* <init>() */
}
override infix fun String.combine(other: String): String {
return <this>.plus(other = other)
}
}
fun box(): String {
with<IntMonoid, Int>(receiver = IntMonoid, block = local fun IntMonoid.<anonymous>(): Int {
return listOf<Int>(elements = [1, 2, 3]).sum<Int>($context_receiver_0 = $this$with)
}
) /*~> Unit */
return with<StringMonoid, String>(receiver = StringMonoid, block = local fun StringMonoid.<anonymous>(): String {
return listOf<String>(elements = ["O", "K"]).sum<String>($context_receiver_0 = $this$with)
}
)
}
fun <T : Any?> List<T>.sum($context_receiver_0: Monoid<T>): T {
return <this>.fold<T, T>(initial = $context_receiver_0.<get-unit>(), operation = local fun <anonymous>(acc: @ParameterName(name = "acc") T, e: T): T {
return ($context_receiver_0, acc).combine(other = e)
}
)
}