Provide average() method for iterables, sequences and arrays.

#KT-3843 Fixed
This commit is contained in:
Ilya Gorbunov
2015-04-16 16:12:05 +03:00
parent f402ae8540
commit 5dacb5a745
4 changed files with 495 additions and 0 deletions
@@ -21,5 +21,23 @@ fun numeric(): List<GenericFunction> {
}
}
templates add f("average()") {
exclude(Strings)
doc { "Returns an average value of elements in the collection"}
returns("Double")
body {
"""
val iterator = iterator()
var sum: Double = 0.0
var count: Int = 0
while (iterator.hasNext()) {
sum += iterator.next()
count += 1
}
return if (count == 0) 0.0 else sum / count
"""
}
}
return templates
}