Introduce sumOf with various selector types

#KT-11253
This commit is contained in:
Ilya Gorbunov
2020-04-26 23:00:08 +03:00
parent bdd53ee9cd
commit 6a24becd1d
13 changed files with 1932 additions and 2 deletions
@@ -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].
*