Skip to content

Commit

Permalink
prevent empty list to fail in sumOf
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerbrandl committed Jul 2, 2024
1 parent 26f72b4 commit 9484ca3
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/main/kotlin/org/kalasim/misc/TimeUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ fun Iterable<Duration>.sum(): Duration = sumOf { it }

/** Compute the sum of a set of durations via a selector function.*/
fun <T> Iterable<T>.sumOf(selector: (T) -> Duration) = map { selector(it) }.run {
reduce { a, b -> a + b }
if(isEmpty()) {
Duration.ZERO
} else {
reduce { a, b -> a + b }
}
}


Expand Down

0 comments on commit 9484ca3

Please sign in to comment.