diff --git a/libraries/stdlib/src/generated/_Aggregates.kt b/libraries/stdlib/src/generated/_Aggregates.kt index 0a9caeeb7a6..97998e11a75 100644 --- a/libraries/stdlib/src/generated/_Aggregates.kt +++ b/libraries/stdlib/src/generated/_Aggregates.kt @@ -2242,3 +2242,267 @@ public inline fun String.reduceRight(operation: (Char, Char) -> Char): Char { return accumulator } +/** + * Returns the sum of all values produced by `transform` function from elements in the collection + */ +public inline fun Array.sumBy(transform: (T) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by `transform` function from elements in the collection + */ +public inline fun BooleanArray.sumBy(transform: (Boolean) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by `transform` function from elements in the collection + */ +public inline fun ByteArray.sumBy(transform: (Byte) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by `transform` function from elements in the collection + */ +public inline fun CharArray.sumBy(transform: (Char) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by `transform` function from elements in the collection + */ +public inline fun DoubleArray.sumBy(transform: (Double) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by `transform` function from elements in the collection + */ +public inline fun FloatArray.sumBy(transform: (Float) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by `transform` function from elements in the collection + */ +public inline fun IntArray.sumBy(transform: (Int) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by `transform` function from elements in the collection + */ +public inline fun LongArray.sumBy(transform: (Long) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by `transform` function from elements in the collection + */ +public inline fun ShortArray.sumBy(transform: (Short) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by `transform` function from elements in the collection + */ +public inline fun Iterable.sumBy(transform: (T) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by `transform` function from elements in the collection + */ +public inline fun Stream.sumBy(transform: (T) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by `transform` function from elements in the collection + */ +public inline fun String.sumBy(transform: (Char) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by `transform` function from elements in the collection + */ +public inline fun Array.sumByDouble(transform: (T) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by `transform` function from elements in the collection + */ +public inline fun BooleanArray.sumByDouble(transform: (Boolean) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by `transform` function from elements in the collection + */ +public inline fun ByteArray.sumByDouble(transform: (Byte) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by `transform` function from elements in the collection + */ +public inline fun CharArray.sumByDouble(transform: (Char) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by `transform` function from elements in the collection + */ +public inline fun DoubleArray.sumByDouble(transform: (Double) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by `transform` function from elements in the collection + */ +public inline fun FloatArray.sumByDouble(transform: (Float) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by `transform` function from elements in the collection + */ +public inline fun IntArray.sumByDouble(transform: (Int) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by `transform` function from elements in the collection + */ +public inline fun LongArray.sumByDouble(transform: (Long) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by `transform` function from elements in the collection + */ +public inline fun ShortArray.sumByDouble(transform: (Short) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by `transform` function from elements in the collection + */ +public inline fun Iterable.sumByDouble(transform: (T) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by `transform` function from elements in the collection + */ +public inline fun Stream.sumByDouble(transform: (T) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by `transform` function from elements in the collection + */ +public inline fun String.sumByDouble(transform: (Char) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += transform(element) + } + return sum +} + diff --git a/libraries/stdlib/test/collections/IterableTests.kt b/libraries/stdlib/test/collections/IterableTests.kt index c972b0ae3d1..b034c1a09d1 100644 --- a/libraries/stdlib/test/collections/IterableTests.kt +++ b/libraries/stdlib/test/collections/IterableTests.kt @@ -202,6 +202,15 @@ abstract class IterableTests>(val data: T, val empty: T) { expect(0) { empty.count { it.startsWith("x") } } } + Test + fun sumBy() { + expect(6) { data.sumBy { it.length() } } + expect(0) { empty.sumBy { it.length() } } + + expect(3.0) { data.sumByDouble { it.length().toDouble() / 2 } } + expect(0.0) { empty.sumByDouble { it.length().toDouble() / 2 } } + } + Test fun withIndices() { var index = 0 diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt index f3cf3f0ddd9..e3484a10d1f 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt @@ -103,6 +103,36 @@ fun aggregates(): List { } } + templates add f("sumBy(transform: (T) -> Int)") { + inline(true) + doc { "Returns the sum of all values produced by `transform` function from elements in the collection" } + returns("Int") + body { + """ + var sum: Int = 0 + for (element in this) { + sum += transform(element) + } + return sum + """ + } + } + + templates add f("sumByDouble(transform: (T) -> Double)") { + inline(true) + doc { "Returns the sum of all values produced by `transform` function from elements in the collection" } + returns("Double") + body { + """ + var sum: Double = 0.0 + for (element in this) { + sum += transform(element) + } + return sum + """ + } + } + templates add f("min()") { doc { "Returns the smallest element or null if there are no elements" } returns("T?")