Brutus5000
10/07/2020, 11:49 AMsumOfOrNull
so I wrote my own very simple one:
inline fun <T> Iterable<T>.sumOfOrNull(selector: (T) -> Double?): Double? =
this.mapNotNull(selector)
.reduceOrNull { acc, i -> acc + i }
Does it make sense to add it to the standard library?
Is this even suitable? I see the standard library uses mostly iterators, for
or while
, and not that much high level stuff (I guess due to performance?)AJ Alt
10/07/2020, 2:59 PMorNull
variant? sumOf
doesn't throw an exception if the iterable is empty.Brutus5000
10/07/2020, 3:23 PMorNull
variant. I am summing over a List<Double?> and if all elements are null, the result should be null not 0. Regular sumOf
would return 0.Brutus5000
10/07/2020, 3:23 PMrnett
10/07/2020, 6:37 PMsumByDouble{ it ?: 0.0 }
Brutus5000
10/07/2020, 8:57 PMnull
and 0
is semantically differentrnett
10/07/2020, 10:06 PMBrutus5000
10/08/2020, 6:46 AMAJ Alt
10/08/2020, 3:19 PM