stdlib: add sumBy(function)
This commit is contained in:
@@ -103,6 +103,36 @@ fun aggregates(): List<GenericFunction> {
|
||||
}
|
||||
}
|
||||
|
||||
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?")
|
||||
|
||||
Reference in New Issue
Block a user