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
@@ -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)