sumBy

public inline fun <T> Iterable<T>.sumBu(selector: (T) -> Int): Int {
    var sum: Int = 0
    for (element in this) {
        sum += selector(element)
    }
    return sum
}


val list = listOf(Person("a", 10), Person("b", 20), Person("c", 30))
println(list.sumBy{ it.age })  // 55

Last updated