Sum a subset of of numbers in a list

You can make use of Iterable<T>.sumBy:

/**
 * Returns the sum of all values produced by [selector] function applied to each element in the collection.
 */
public inline fun <T> Iterable<T>.sumBy(selector: (T) -> Int): Int {
    var sum: Int = 0
    for (element in this) {
        sum += selector(element)
    }
    return sum
}

You can pass a function to it where the function transforms negative value to 0. So, it sums up all values in the list which is greater than 0 since adding 0 makes no effect to the result.

val nums = listOf<Long>(-2, -1, 1, 2, 3, 4)
val sum = nums.sumBy { if (it > 0) it.toInt() else 0 }
println(sum)    //10

If you require a Long value back, you have to write an extension for Long just like Iterable<T>.sumByDouble.

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

Then, the toInt() conversion can be taken away.

 nums.sumByLong { if (it > 0) it else 0 }

As suggested by @Ruckus T-Boom, if (it > 0) it else 0 can be simplified using Long.coerceAtLeast() which returns the value itself or the given minimum value:

nums.sumByLong { it.coerceAtLeast(0) }

sumBy and sumByDouble are Deprecated from kotlin 1.5 . You can check those link.

Use sumOf to get sum on a List or Array

sumOf

Returns the sum of all values produced by selector function applied to each element in the collection or Array.

Example:

data class Order(
  val id : String,
  val price : Double
) 

val orderList = ......
val sum = orderList.sumOf { it.price }

Tags:

Kotlin