stdlib: add sumBy(function)

This commit is contained in:
Ilya Ryzhenkov
2015-01-13 19:35:19 +03:00
parent 4ee64d7283
commit 1d4591d99c
3 changed files with 303 additions and 0 deletions
@@ -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?")