From 6a24becd1d7ade3156363667f701128666b4d3f3 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Sun, 26 Apr 2020 23:00:08 +0300 Subject: [PATCH] Introduce sumOf with various selector types #KT-11253 --- .../stdlib/common/src/generated/_Arrays.kt | 738 ++++++++++++++++++ .../common/src/generated/_Collections.kt | 82 ++ .../stdlib/common/src/generated/_Sequences.kt | 92 +++ .../stdlib/common/src/generated/_Strings.kt | 82 ++ .../stdlib/common/src/generated/_UArrays.kt | 340 ++++++++ .../stdlib/jvm/src/generated/_ArraysJvm.kt | 288 +++++++ .../jvm/src/generated/_CollectionsJvm.kt | 32 + .../stdlib/jvm/src/generated/_SequencesJvm.kt | 36 + .../stdlib/jvm/src/generated/_StringsJvm.kt | 32 + .../stdlib/jvm/src/generated/_UArraysJvm.kt | 136 ++++ .../stdlib/jvm/test/numbers/BigNumbersTest.kt | 24 + .../stdlib/test/collections/CollectionTest.kt | 18 +- .../src/templates/Aggregates.kt | 34 +- 13 files changed, 1932 insertions(+), 2 deletions(-) diff --git a/libraries/stdlib/common/src/generated/_Arrays.kt b/libraries/stdlib/common/src/generated/_Arrays.kt index d670e8686c9..431e2bca833 100644 --- a/libraries/stdlib/common/src/generated/_Arrays.kt +++ b/libraries/stdlib/common/src/generated/_Arrays.kt @@ -17483,6 +17483,744 @@ public inline fun CharArray.sumByDouble(selector: (Char) -> Double): Double { return sum } +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfDouble") +@kotlin.internal.InlineOnly +public inline fun Array.sumOf(selector: (T) -> Double): Double { + var sum: Double = 0.toDouble() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfDouble") +@kotlin.internal.InlineOnly +public inline fun ByteArray.sumOf(selector: (Byte) -> Double): Double { + var sum: Double = 0.toDouble() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfDouble") +@kotlin.internal.InlineOnly +public inline fun ShortArray.sumOf(selector: (Short) -> Double): Double { + var sum: Double = 0.toDouble() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfDouble") +@kotlin.internal.InlineOnly +public inline fun IntArray.sumOf(selector: (Int) -> Double): Double { + var sum: Double = 0.toDouble() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfDouble") +@kotlin.internal.InlineOnly +public inline fun LongArray.sumOf(selector: (Long) -> Double): Double { + var sum: Double = 0.toDouble() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfDouble") +@kotlin.internal.InlineOnly +public inline fun FloatArray.sumOf(selector: (Float) -> Double): Double { + var sum: Double = 0.toDouble() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfDouble") +@kotlin.internal.InlineOnly +public inline fun DoubleArray.sumOf(selector: (Double) -> Double): Double { + var sum: Double = 0.toDouble() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfDouble") +@kotlin.internal.InlineOnly +public inline fun BooleanArray.sumOf(selector: (Boolean) -> Double): Double { + var sum: Double = 0.toDouble() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfDouble") +@kotlin.internal.InlineOnly +public inline fun CharArray.sumOf(selector: (Char) -> Double): Double { + var sum: Double = 0.toDouble() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfInt") +@kotlin.internal.InlineOnly +public inline fun Array.sumOf(selector: (T) -> Int): Int { + var sum: Int = 0.toInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfInt") +@kotlin.internal.InlineOnly +public inline fun ByteArray.sumOf(selector: (Byte) -> Int): Int { + var sum: Int = 0.toInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfInt") +@kotlin.internal.InlineOnly +public inline fun ShortArray.sumOf(selector: (Short) -> Int): Int { + var sum: Int = 0.toInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfInt") +@kotlin.internal.InlineOnly +public inline fun IntArray.sumOf(selector: (Int) -> Int): Int { + var sum: Int = 0.toInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfInt") +@kotlin.internal.InlineOnly +public inline fun LongArray.sumOf(selector: (Long) -> Int): Int { + var sum: Int = 0.toInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfInt") +@kotlin.internal.InlineOnly +public inline fun FloatArray.sumOf(selector: (Float) -> Int): Int { + var sum: Int = 0.toInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfInt") +@kotlin.internal.InlineOnly +public inline fun DoubleArray.sumOf(selector: (Double) -> Int): Int { + var sum: Int = 0.toInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfInt") +@kotlin.internal.InlineOnly +public inline fun BooleanArray.sumOf(selector: (Boolean) -> Int): Int { + var sum: Int = 0.toInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfInt") +@kotlin.internal.InlineOnly +public inline fun CharArray.sumOf(selector: (Char) -> Int): Int { + var sum: Int = 0.toInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfLong") +@kotlin.internal.InlineOnly +public inline fun Array.sumOf(selector: (T) -> Long): Long { + var sum: Long = 0.toLong() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfLong") +@kotlin.internal.InlineOnly +public inline fun ByteArray.sumOf(selector: (Byte) -> Long): Long { + var sum: Long = 0.toLong() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfLong") +@kotlin.internal.InlineOnly +public inline fun ShortArray.sumOf(selector: (Short) -> Long): Long { + var sum: Long = 0.toLong() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfLong") +@kotlin.internal.InlineOnly +public inline fun IntArray.sumOf(selector: (Int) -> Long): Long { + var sum: Long = 0.toLong() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfLong") +@kotlin.internal.InlineOnly +public inline fun LongArray.sumOf(selector: (Long) -> Long): Long { + var sum: Long = 0.toLong() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfLong") +@kotlin.internal.InlineOnly +public inline fun FloatArray.sumOf(selector: (Float) -> Long): Long { + var sum: Long = 0.toLong() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfLong") +@kotlin.internal.InlineOnly +public inline fun DoubleArray.sumOf(selector: (Double) -> Long): Long { + var sum: Long = 0.toLong() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfLong") +@kotlin.internal.InlineOnly +public inline fun BooleanArray.sumOf(selector: (Boolean) -> Long): Long { + var sum: Long = 0.toLong() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfLong") +@kotlin.internal.InlineOnly +public inline fun CharArray.sumOf(selector: (Char) -> Long): Long { + var sum: Long = 0.toLong() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfUInt") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun Array.sumOf(selector: (T) -> UInt): UInt { + var sum: UInt = 0.toUInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfUInt") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ByteArray.sumOf(selector: (Byte) -> UInt): UInt { + var sum: UInt = 0.toUInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfUInt") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ShortArray.sumOf(selector: (Short) -> UInt): UInt { + var sum: UInt = 0.toUInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfUInt") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun IntArray.sumOf(selector: (Int) -> UInt): UInt { + var sum: UInt = 0.toUInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfUInt") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun LongArray.sumOf(selector: (Long) -> UInt): UInt { + var sum: UInt = 0.toUInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfUInt") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun FloatArray.sumOf(selector: (Float) -> UInt): UInt { + var sum: UInt = 0.toUInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfUInt") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun DoubleArray.sumOf(selector: (Double) -> UInt): UInt { + var sum: UInt = 0.toUInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfUInt") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun BooleanArray.sumOf(selector: (Boolean) -> UInt): UInt { + var sum: UInt = 0.toUInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfUInt") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun CharArray.sumOf(selector: (Char) -> UInt): UInt { + var sum: UInt = 0.toUInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfULong") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun Array.sumOf(selector: (T) -> ULong): ULong { + var sum: ULong = 0.toULong() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfULong") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ByteArray.sumOf(selector: (Byte) -> ULong): ULong { + var sum: ULong = 0.toULong() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfULong") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ShortArray.sumOf(selector: (Short) -> ULong): ULong { + var sum: ULong = 0.toULong() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfULong") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun IntArray.sumOf(selector: (Int) -> ULong): ULong { + var sum: ULong = 0.toULong() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfULong") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun LongArray.sumOf(selector: (Long) -> ULong): ULong { + var sum: ULong = 0.toULong() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfULong") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun FloatArray.sumOf(selector: (Float) -> ULong): ULong { + var sum: ULong = 0.toULong() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfULong") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun DoubleArray.sumOf(selector: (Double) -> ULong): ULong { + var sum: ULong = 0.toULong() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfULong") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun BooleanArray.sumOf(selector: (Boolean) -> ULong): ULong { + var sum: ULong = 0.toULong() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfULong") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun CharArray.sumOf(selector: (Char) -> ULong): ULong { + var sum: ULong = 0.toULong() + for (element in this) { + sum += selector(element) + } + return sum +} + /** * Returns an original collection containing all the non-`null` elements, throwing an [IllegalArgumentException] if there are any `null` elements. */ diff --git a/libraries/stdlib/common/src/generated/_Collections.kt b/libraries/stdlib/common/src/generated/_Collections.kt index af6ec87cda8..ab85530f626 100644 --- a/libraries/stdlib/common/src/generated/_Collections.kt +++ b/libraries/stdlib/common/src/generated/_Collections.kt @@ -2293,6 +2293,88 @@ public inline fun Iterable.sumByDouble(selector: (T) -> Double): Double { return sum } +/** + * Returns the sum of all values produced by [selector] function applied to each element in the collection. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfDouble") +@kotlin.internal.InlineOnly +public inline fun Iterable.sumOf(selector: (T) -> Double): Double { + var sum: Double = 0.toDouble() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the collection. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfInt") +@kotlin.internal.InlineOnly +public inline fun Iterable.sumOf(selector: (T) -> Int): Int { + var sum: Int = 0.toInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the collection. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfLong") +@kotlin.internal.InlineOnly +public inline fun Iterable.sumOf(selector: (T) -> Long): Long { + var sum: Long = 0.toLong() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the collection. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfUInt") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun Iterable.sumOf(selector: (T) -> UInt): UInt { + var sum: UInt = 0.toUInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the collection. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfULong") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun Iterable.sumOf(selector: (T) -> ULong): ULong { + var sum: ULong = 0.toULong() + for (element in this) { + sum += selector(element) + } + return sum +} + /** * Returns an original collection containing all the non-`null` elements, throwing an [IllegalArgumentException] if there are any `null` elements. */ diff --git a/libraries/stdlib/common/src/generated/_Sequences.kt b/libraries/stdlib/common/src/generated/_Sequences.kt index e90b8fbc159..2c7ac55b7f2 100644 --- a/libraries/stdlib/common/src/generated/_Sequences.kt +++ b/libraries/stdlib/common/src/generated/_Sequences.kt @@ -1710,6 +1710,98 @@ public inline fun Sequence.sumByDouble(selector: (T) -> Double): Double { return sum } +/** + * Returns the sum of all values produced by [selector] function applied to each element in the sequence. + * + * The operation is _terminal_. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfDouble") +@kotlin.internal.InlineOnly +public inline fun Sequence.sumOf(selector: (T) -> Double): Double { + var sum: Double = 0.toDouble() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the sequence. + * + * The operation is _terminal_. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfInt") +@kotlin.internal.InlineOnly +public inline fun Sequence.sumOf(selector: (T) -> Int): Int { + var sum: Int = 0.toInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the sequence. + * + * The operation is _terminal_. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfLong") +@kotlin.internal.InlineOnly +public inline fun Sequence.sumOf(selector: (T) -> Long): Long { + var sum: Long = 0.toLong() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the sequence. + * + * The operation is _terminal_. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfUInt") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun Sequence.sumOf(selector: (T) -> UInt): UInt { + var sum: UInt = 0.toUInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the sequence. + * + * The operation is _terminal_. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfULong") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun Sequence.sumOf(selector: (T) -> ULong): ULong { + var sum: ULong = 0.toULong() + for (element in this) { + sum += selector(element) + } + return sum +} + /** * Returns an original collection containing all the non-`null` elements, throwing an [IllegalArgumentException] if there are any `null` elements. * diff --git a/libraries/stdlib/common/src/generated/_Strings.kt b/libraries/stdlib/common/src/generated/_Strings.kt index 64e5e4c7edc..309e204c5af 100644 --- a/libraries/stdlib/common/src/generated/_Strings.kt +++ b/libraries/stdlib/common/src/generated/_Strings.kt @@ -1567,6 +1567,88 @@ public inline fun CharSequence.sumByDouble(selector: (Char) -> Double): Double { return sum } +/** + * Returns the sum of all values produced by [selector] function applied to each character in the char sequence. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfDouble") +@kotlin.internal.InlineOnly +public inline fun CharSequence.sumOf(selector: (Char) -> Double): Double { + var sum: Double = 0.toDouble() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each character in the char sequence. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfInt") +@kotlin.internal.InlineOnly +public inline fun CharSequence.sumOf(selector: (Char) -> Int): Int { + var sum: Int = 0.toInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each character in the char sequence. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfLong") +@kotlin.internal.InlineOnly +public inline fun CharSequence.sumOf(selector: (Char) -> Long): Long { + var sum: Long = 0.toLong() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each character in the char sequence. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfUInt") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun CharSequence.sumOf(selector: (Char) -> UInt): UInt { + var sum: UInt = 0.toUInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each character in the char sequence. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfULong") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun CharSequence.sumOf(selector: (Char) -> ULong): ULong { + var sum: ULong = 0.toULong() + for (element in this) { + sum += selector(element) + } + return sum +} + /** * Splits this char sequence into a list of strings each not exceeding the given [size]. * diff --git a/libraries/stdlib/common/src/generated/_UArrays.kt b/libraries/stdlib/common/src/generated/_UArrays.kt index 21359111a34..83084848796 100644 --- a/libraries/stdlib/common/src/generated/_UArrays.kt +++ b/libraries/stdlib/common/src/generated/_UArrays.kt @@ -7890,6 +7890,346 @@ public inline fun UShortArray.sumByDouble(selector: (UShort) -> Double): Double return sum } +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfDouble") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.sumOf(selector: (UInt) -> Double): Double { + var sum: Double = 0.toDouble() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfDouble") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.sumOf(selector: (ULong) -> Double): Double { + var sum: Double = 0.toDouble() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfDouble") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.sumOf(selector: (UByte) -> Double): Double { + var sum: Double = 0.toDouble() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfDouble") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.sumOf(selector: (UShort) -> Double): Double { + var sum: Double = 0.toDouble() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfInt") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.sumOf(selector: (UInt) -> Int): Int { + var sum: Int = 0.toInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfInt") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.sumOf(selector: (ULong) -> Int): Int { + var sum: Int = 0.toInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfInt") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.sumOf(selector: (UByte) -> Int): Int { + var sum: Int = 0.toInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfInt") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.sumOf(selector: (UShort) -> Int): Int { + var sum: Int = 0.toInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfLong") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.sumOf(selector: (UInt) -> Long): Long { + var sum: Long = 0.toLong() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfLong") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.sumOf(selector: (ULong) -> Long): Long { + var sum: Long = 0.toLong() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfLong") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.sumOf(selector: (UByte) -> Long): Long { + var sum: Long = 0.toLong() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfLong") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.sumOf(selector: (UShort) -> Long): Long { + var sum: Long = 0.toLong() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfUInt") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.sumOf(selector: (UInt) -> UInt): UInt { + var sum: UInt = 0.toUInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfUInt") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.sumOf(selector: (ULong) -> UInt): UInt { + var sum: UInt = 0.toUInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfUInt") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.sumOf(selector: (UByte) -> UInt): UInt { + var sum: UInt = 0.toUInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfUInt") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.sumOf(selector: (UShort) -> UInt): UInt { + var sum: UInt = 0.toUInt() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfULong") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.sumOf(selector: (UInt) -> ULong): ULong { + var sum: ULong = 0.toULong() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfULong") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.sumOf(selector: (ULong) -> ULong): ULong { + var sum: ULong = 0.toULong() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfULong") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.sumOf(selector: (UByte) -> ULong): ULong { + var sum: ULong = 0.toULong() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfULong") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.sumOf(selector: (UShort) -> ULong): ULong { + var sum: ULong = 0.toULong() + for (element in this) { + sum += selector(element) + } + return sum +} + /** * Returns a list of pairs built from the elements of `this` array and the [other] array with the same index. * The returned list has length of the shortest collection. diff --git a/libraries/stdlib/jvm/src/generated/_ArraysJvm.kt b/libraries/stdlib/jvm/src/generated/_ArraysJvm.kt index 8282dd2eec7..f2c60bc2486 100644 --- a/libraries/stdlib/jvm/src/generated/_ArraysJvm.kt +++ b/libraries/stdlib/jvm/src/generated/_ArraysJvm.kt @@ -2702,3 +2702,291 @@ public fun Array.toSortedSet(comparator: Comparator): java.util return toCollection(java.util.TreeSet(comparator)) } +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigDecimal") +@kotlin.internal.InlineOnly +public inline fun Array.sumOf(selector: (T) -> java.math.BigDecimal): java.math.BigDecimal { + var sum: java.math.BigDecimal = 0.toBigDecimal() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigDecimal") +@kotlin.internal.InlineOnly +public inline fun ByteArray.sumOf(selector: (Byte) -> java.math.BigDecimal): java.math.BigDecimal { + var sum: java.math.BigDecimal = 0.toBigDecimal() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigDecimal") +@kotlin.internal.InlineOnly +public inline fun ShortArray.sumOf(selector: (Short) -> java.math.BigDecimal): java.math.BigDecimal { + var sum: java.math.BigDecimal = 0.toBigDecimal() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigDecimal") +@kotlin.internal.InlineOnly +public inline fun IntArray.sumOf(selector: (Int) -> java.math.BigDecimal): java.math.BigDecimal { + var sum: java.math.BigDecimal = 0.toBigDecimal() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigDecimal") +@kotlin.internal.InlineOnly +public inline fun LongArray.sumOf(selector: (Long) -> java.math.BigDecimal): java.math.BigDecimal { + var sum: java.math.BigDecimal = 0.toBigDecimal() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigDecimal") +@kotlin.internal.InlineOnly +public inline fun FloatArray.sumOf(selector: (Float) -> java.math.BigDecimal): java.math.BigDecimal { + var sum: java.math.BigDecimal = 0.toBigDecimal() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigDecimal") +@kotlin.internal.InlineOnly +public inline fun DoubleArray.sumOf(selector: (Double) -> java.math.BigDecimal): java.math.BigDecimal { + var sum: java.math.BigDecimal = 0.toBigDecimal() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigDecimal") +@kotlin.internal.InlineOnly +public inline fun BooleanArray.sumOf(selector: (Boolean) -> java.math.BigDecimal): java.math.BigDecimal { + var sum: java.math.BigDecimal = 0.toBigDecimal() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigDecimal") +@kotlin.internal.InlineOnly +public inline fun CharArray.sumOf(selector: (Char) -> java.math.BigDecimal): java.math.BigDecimal { + var sum: java.math.BigDecimal = 0.toBigDecimal() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigInteger") +@kotlin.internal.InlineOnly +public inline fun Array.sumOf(selector: (T) -> java.math.BigInteger): java.math.BigInteger { + var sum: java.math.BigInteger = 0.toBigInteger() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigInteger") +@kotlin.internal.InlineOnly +public inline fun ByteArray.sumOf(selector: (Byte) -> java.math.BigInteger): java.math.BigInteger { + var sum: java.math.BigInteger = 0.toBigInteger() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigInteger") +@kotlin.internal.InlineOnly +public inline fun ShortArray.sumOf(selector: (Short) -> java.math.BigInteger): java.math.BigInteger { + var sum: java.math.BigInteger = 0.toBigInteger() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigInteger") +@kotlin.internal.InlineOnly +public inline fun IntArray.sumOf(selector: (Int) -> java.math.BigInteger): java.math.BigInteger { + var sum: java.math.BigInteger = 0.toBigInteger() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigInteger") +@kotlin.internal.InlineOnly +public inline fun LongArray.sumOf(selector: (Long) -> java.math.BigInteger): java.math.BigInteger { + var sum: java.math.BigInteger = 0.toBigInteger() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigInteger") +@kotlin.internal.InlineOnly +public inline fun FloatArray.sumOf(selector: (Float) -> java.math.BigInteger): java.math.BigInteger { + var sum: java.math.BigInteger = 0.toBigInteger() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigInteger") +@kotlin.internal.InlineOnly +public inline fun DoubleArray.sumOf(selector: (Double) -> java.math.BigInteger): java.math.BigInteger { + var sum: java.math.BigInteger = 0.toBigInteger() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigInteger") +@kotlin.internal.InlineOnly +public inline fun BooleanArray.sumOf(selector: (Boolean) -> java.math.BigInteger): java.math.BigInteger { + var sum: java.math.BigInteger = 0.toBigInteger() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigInteger") +@kotlin.internal.InlineOnly +public inline fun CharArray.sumOf(selector: (Char) -> java.math.BigInteger): java.math.BigInteger { + var sum: java.math.BigInteger = 0.toBigInteger() + for (element in this) { + sum += selector(element) + } + return sum +} + diff --git a/libraries/stdlib/jvm/src/generated/_CollectionsJvm.kt b/libraries/stdlib/jvm/src/generated/_CollectionsJvm.kt index 2822c9924f2..4202b50e280 100644 --- a/libraries/stdlib/jvm/src/generated/_CollectionsJvm.kt +++ b/libraries/stdlib/jvm/src/generated/_CollectionsJvm.kt @@ -55,3 +55,35 @@ public fun Iterable.toSortedSet(comparator: Comparator): java.util. return toCollection(java.util.TreeSet(comparator)) } +/** + * Returns the sum of all values produced by [selector] function applied to each element in the collection. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigDecimal") +@kotlin.internal.InlineOnly +public inline fun Iterable.sumOf(selector: (T) -> java.math.BigDecimal): java.math.BigDecimal { + var sum: java.math.BigDecimal = 0.toBigDecimal() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the collection. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigInteger") +@kotlin.internal.InlineOnly +public inline fun Iterable.sumOf(selector: (T) -> java.math.BigInteger): java.math.BigInteger { + var sum: java.math.BigInteger = 0.toBigInteger() + for (element in this) { + sum += selector(element) + } + return sum +} + diff --git a/libraries/stdlib/jvm/src/generated/_SequencesJvm.kt b/libraries/stdlib/jvm/src/generated/_SequencesJvm.kt index 5847a739370..06ebc1afb5e 100644 --- a/libraries/stdlib/jvm/src/generated/_SequencesJvm.kt +++ b/libraries/stdlib/jvm/src/generated/_SequencesJvm.kt @@ -55,3 +55,39 @@ public fun Sequence.toSortedSet(comparator: Comparator): java.util. return toCollection(java.util.TreeSet(comparator)) } +/** + * Returns the sum of all values produced by [selector] function applied to each element in the sequence. + * + * The operation is _terminal_. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigDecimal") +@kotlin.internal.InlineOnly +public inline fun Sequence.sumOf(selector: (T) -> java.math.BigDecimal): java.math.BigDecimal { + var sum: java.math.BigDecimal = 0.toBigDecimal() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the sequence. + * + * The operation is _terminal_. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigInteger") +@kotlin.internal.InlineOnly +public inline fun Sequence.sumOf(selector: (T) -> java.math.BigInteger): java.math.BigInteger { + var sum: java.math.BigInteger = 0.toBigInteger() + for (element in this) { + sum += selector(element) + } + return sum +} + diff --git a/libraries/stdlib/jvm/src/generated/_StringsJvm.kt b/libraries/stdlib/jvm/src/generated/_StringsJvm.kt index c000bb544ed..4889fc842fc 100644 --- a/libraries/stdlib/jvm/src/generated/_StringsJvm.kt +++ b/libraries/stdlib/jvm/src/generated/_StringsJvm.kt @@ -31,3 +31,35 @@ public fun CharSequence.toSortedSet(): java.util.SortedSet { return toCollection(java.util.TreeSet()) } +/** + * Returns the sum of all values produced by [selector] function applied to each character in the char sequence. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigDecimal") +@kotlin.internal.InlineOnly +public inline fun CharSequence.sumOf(selector: (Char) -> java.math.BigDecimal): java.math.BigDecimal { + var sum: java.math.BigDecimal = 0.toBigDecimal() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each character in the char sequence. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigInteger") +@kotlin.internal.InlineOnly +public inline fun CharSequence.sumOf(selector: (Char) -> java.math.BigInteger): java.math.BigInteger { + var sum: java.math.BigInteger = 0.toBigInteger() + for (element in this) { + sum += selector(element) + } + return sum +} + diff --git a/libraries/stdlib/jvm/src/generated/_UArraysJvm.kt b/libraries/stdlib/jvm/src/generated/_UArraysJvm.kt index f69468e33ee..f5d2f7aac4c 100644 --- a/libraries/stdlib/jvm/src/generated/_UArraysJvm.kt +++ b/libraries/stdlib/jvm/src/generated/_UArraysJvm.kt @@ -285,3 +285,139 @@ public fun UShortArray.binarySearch(element: UShort, fromIndex: Int = 0, toIndex return -(low + 1) // key not found } +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigDecimal") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.sumOf(selector: (UInt) -> java.math.BigDecimal): java.math.BigDecimal { + var sum: java.math.BigDecimal = 0.toBigDecimal() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigDecimal") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.sumOf(selector: (ULong) -> java.math.BigDecimal): java.math.BigDecimal { + var sum: java.math.BigDecimal = 0.toBigDecimal() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigDecimal") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.sumOf(selector: (UByte) -> java.math.BigDecimal): java.math.BigDecimal { + var sum: java.math.BigDecimal = 0.toBigDecimal() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigDecimal") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.sumOf(selector: (UShort) -> java.math.BigDecimal): java.math.BigDecimal { + var sum: java.math.BigDecimal = 0.toBigDecimal() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigInteger") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.sumOf(selector: (UInt) -> java.math.BigInteger): java.math.BigInteger { + var sum: java.math.BigInteger = 0.toBigInteger() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigInteger") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.sumOf(selector: (ULong) -> java.math.BigInteger): java.math.BigInteger { + var sum: java.math.BigInteger = 0.toBigInteger() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigInteger") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.sumOf(selector: (UByte) -> java.math.BigInteger): java.math.BigInteger { + var sum: java.math.BigInteger = 0.toBigInteger() + for (element in this) { + sum += selector(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [selector] function applied to each element in the array. + */ +@SinceKotlin("1.4") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +@kotlin.jvm.JvmName("sumOfBigInteger") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.sumOf(selector: (UShort) -> java.math.BigInteger): java.math.BigInteger { + var sum: java.math.BigInteger = 0.toBigInteger() + for (element in this) { + sum += selector(element) + } + return sum +} + diff --git a/libraries/stdlib/jvm/test/numbers/BigNumbersTest.kt b/libraries/stdlib/jvm/test/numbers/BigNumbersTest.kt index 8096f396e2f..e811a565cc6 100644 --- a/libraries/stdlib/jvm/test/numbers/BigNumbersTest.kt +++ b/libraries/stdlib/jvm/test/numbers/BigNumbersTest.kt @@ -62,6 +62,18 @@ class BigNumbersTest { assertEquals(BigInteger("2"), c) } + @Test fun sumOfBigInteger() { + val numbers = (1..10).map { it.toBigInteger() } + val i55 = 55.toBigInteger() + assertEquals(i55, numbers.sumOf { it }) + assertEquals(i55, numbers.asSequence().sumOf { it }) + assertEquals(i55, numbers.toTypedArray().sumOf { it }) + + val chars = ('0'..'9').joinToString("") + assertEquals(i55, chars.sumOf { it.toString().toBigInteger().inc() }) + assertEquals(i55, chars.toCharArray().sumOf { it.toString().toBigInteger().inc() }) + } + @Test fun testBigDecimal() { val a = BigDecimal("2") val b = BigDecimal("3") @@ -103,5 +115,17 @@ class BigNumbersTest { assertEquals(d4, d7 / d2) assertEquals(d1, d7 / d5) } + + @Test fun sumOfBigDecimal() { + val numbers = (1..10).map { it.toBigDecimal() } + val d55 = 55.toBigDecimal() + assertEquals(d55, numbers.sumOf { it }) + assertEquals(d55, numbers.asSequence().sumOf { it }) + assertEquals(d55, numbers.toTypedArray().sumOf { it }) + + val chars = ('0'..'9').joinToString("") + assertEquals(d55, chars.sumOf { it.toString().toBigDecimal().inc() }) + assertEquals(d55, chars.toCharArray().sumOf { it.toString().toBigDecimal().inc() }) + } } diff --git a/libraries/stdlib/test/collections/CollectionTest.kt b/libraries/stdlib/test/collections/CollectionTest.kt index f6af6c1447a..63de7c0afbf 100644 --- a/libraries/stdlib/test/collections/CollectionTest.kt +++ b/libraries/stdlib/test/collections/CollectionTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -903,6 +903,22 @@ class CollectionTest { expect(3.0.toFloat()) { sequenceOf(1.0.toFloat(), 2.0.toFloat()).sum() } } + @Test fun sumOf() { + assertEquals(0, emptyList().sumOf { 1.toInt() }) + assertEquals(0L, emptyList().sumOf { 1L }) + assertEquals(0U, emptyList().sumOf { 1U.toUInt() }) + assertEquals(0UL, emptyList().sumOf { 1UL }) + assertEquals(0.0, emptyList().sumOf { 1.0 }) + + val items = listOf("", "a", "bc", "de", "fgh", "klmnop") + assertEquals(items.size + 14, items.sumOf { it.length + 1 }) + assertEquals(14L, items.sumOf { it.length.toLong() }) + assertEquals(items.size.toUInt(), items.sumOf { 1U.toUInt() }) + assertEquals(14UL, items.sumOf { it.length.toULong() }) + assertEquals(14.0, items.sumOf { it.length.toDouble() }) + assertEquals(Double.NaN, items.sumOf { 0.0 / it.length }) + } + @Test fun average() { assertTrue { arrayListOf().average().isNaN() } expect(3.8) { listOf(1, 2, 5, 8, 3).average() } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt index a85db01e23f..9b0a456253f 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -250,6 +250,38 @@ object Aggregates : TemplateGroupBase() { } } + fun f_sumOf() = listOf("Int", "Long", "UInt", "ULong", "Double", "java.math.BigInteger", "java.math.BigDecimal").map { selectorType -> + fn("sumOf(selector: (T) -> $selectorType)") { + includeDefault() + include(CharSequences, ArraysOfUnsigned) + if (selectorType.startsWith("java")) platforms(Platform.JVM) + } builder { + inlineOnly() + since("1.4") + val typeShortName = when { + selectorType.startsWith("java") -> selectorType.substringAfterLast('.') + else -> selectorType + } + annotation("@OptIn(kotlin.experimental.ExperimentalTypeInference::class)") + annotation("@OverloadResolutionByLambdaReturnType") + annotation("""@kotlin.jvm.JvmName("sumOf$typeShortName")""") // should not be needed if inline return type is mangled + if (selectorType.startsWith("U")) + annotation("@ExperimentalUnsignedTypes") + + doc { "Returns the sum of all values produced by [selector] function applied to each ${f.element} in the ${f.collection}." } + returns(selectorType) + body { + """ + var sum: $selectorType = 0.to$typeShortName() + for (element in this) { + sum += selector(element) + } + return sum + """ + } + } + } + val f_sumByDouble = fn("sumByDouble(selector: (T) -> Double)") { includeDefault() include(CharSequences, ArraysOfUnsigned)