Jonathan Ellis
05/27/2022, 7:03 PMephemient
05/28/2022, 6:20 AMtrait Add<T> { type Output; operator fun plus(T): Output }
fun <T, R : Add<R>> Iterable<T>.sumOf(selector: (T) -> R): R where Add<T>::Output : Rfun <T, R> Iterable.sumOf(dictionary: { plus: R.(R) -> R }, selector: (T) -> R): RYoussef Shoaib [MOD]
05/28/2022, 4:39 PMinterface Add<T> {
    operator fun T.plus(other: T): T
}
object IntAdd : Add<Int> {
    override fun Int.plus(other: Int): Int {
        return this + other // This actually calls the member Int.plus
    }
}
context(Add<R>)
fun <T, R> Iterable<T>.sumOf(selector: (T) -> R): R? {
    var sum: R? = null
    for (element in this) {
        sum = if (sum == null) {
            selector(element)
        } else {
            sum + selector(element)
        }
    }
    return sum
}
context(Add<T>)
fun <T> Iterable<T>.sum(): T? {
    return sumOf { it }
}
fun test(){
    with(IntAdd){
        // Because of the context, this takes priority over the
        // context-less extensions defined in stdlib
        println(listOf(40, 42, 44).sum()!! / 3)
        println(listOf("hello", "test", "elo mate").sumOf(String::length))
    }
}Monoidephemient
05/28/2022, 4:46 PMAdd<T>dictionaryYoussef Shoaib [MOD]
05/28/2022, 5:08 PMListAddListIntAddListStringAddplusTTArrayListArrayListobject ListAddfun listAdd<T>() : Add<List<T>> = ListAdd as Add<List<T>>context-provider-magic-syntax fun <T> listAdd(): Add<List<T>> = ListAdd as Add<List<T>>Add<List<whatever>>with SomeObjectephemient
05/28/2022, 8:34 PMDan Fingal-Surma
05/30/2022, 6:02 PMdictionary: { plus: R.(R) -> R }ephemient
05/30/2022, 6:04 PMAdd<T>ephemient
05/30/2022, 6:05 PMDan Fingal-Surma
05/30/2022, 6:30 PMephemient
05/30/2022, 6:31 PMDan Fingal-Surma
05/30/2022, 7:03 PMephemient
05/30/2022, 7:06 PMDan Fingal-Surma
05/30/2022, 11:32 PMDan Fingal-Surma
05/30/2022, 11:36 PM