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
@@ -2242,3 +2242,267 @@ public inline fun String.reduceRight(operation: (Char, Char) -> Char): Char {
return accumulator
}
/**
* Returns the sum of all values produced by `transform` function from elements in the collection
*/
public inline fun <T> Array<out T>.sumBy(transform: (T) -> Int): Int {
var sum: Int = 0
for (element in this) {
sum += transform(element)
}
return sum
}
/**
* Returns the sum of all values produced by `transform` function from elements in the collection
*/
public inline fun BooleanArray.sumBy(transform: (Boolean) -> Int): Int {
var sum: Int = 0
for (element in this) {
sum += transform(element)
}
return sum
}
/**
* Returns the sum of all values produced by `transform` function from elements in the collection
*/
public inline fun ByteArray.sumBy(transform: (Byte) -> Int): Int {
var sum: Int = 0
for (element in this) {
sum += transform(element)
}
return sum
}
/**
* Returns the sum of all values produced by `transform` function from elements in the collection
*/
public inline fun CharArray.sumBy(transform: (Char) -> Int): Int {
var sum: Int = 0
for (element in this) {
sum += transform(element)
}
return sum
}
/**
* Returns the sum of all values produced by `transform` function from elements in the collection
*/
public inline fun DoubleArray.sumBy(transform: (Double) -> Int): Int {
var sum: Int = 0
for (element in this) {
sum += transform(element)
}
return sum
}
/**
* Returns the sum of all values produced by `transform` function from elements in the collection
*/
public inline fun FloatArray.sumBy(transform: (Float) -> Int): Int {
var sum: Int = 0
for (element in this) {
sum += transform(element)
}
return sum
}
/**
* Returns the sum of all values produced by `transform` function from elements in the collection
*/
public inline fun IntArray.sumBy(transform: (Int) -> Int): Int {
var sum: Int = 0
for (element in this) {
sum += transform(element)
}
return sum
}
/**
* Returns the sum of all values produced by `transform` function from elements in the collection
*/
public inline fun LongArray.sumBy(transform: (Long) -> Int): Int {
var sum: Int = 0
for (element in this) {
sum += transform(element)
}
return sum
}
/**
* Returns the sum of all values produced by `transform` function from elements in the collection
*/
public inline fun ShortArray.sumBy(transform: (Short) -> Int): Int {
var sum: Int = 0
for (element in this) {
sum += transform(element)
}
return sum
}
/**
* Returns the sum of all values produced by `transform` function from elements in the collection
*/
public inline fun <T> Iterable<T>.sumBy(transform: (T) -> Int): Int {
var sum: Int = 0
for (element in this) {
sum += transform(element)
}
return sum
}
/**
* Returns the sum of all values produced by `transform` function from elements in the collection
*/
public inline fun <T> Stream<T>.sumBy(transform: (T) -> Int): Int {
var sum: Int = 0
for (element in this) {
sum += transform(element)
}
return sum
}
/**
* Returns the sum of all values produced by `transform` function from elements in the collection
*/
public inline fun String.sumBy(transform: (Char) -> Int): Int {
var sum: Int = 0
for (element in this) {
sum += transform(element)
}
return sum
}
/**
* Returns the sum of all values produced by `transform` function from elements in the collection
*/
public inline fun <T> Array<out T>.sumByDouble(transform: (T) -> Double): Double {
var sum: Double = 0.0
for (element in this) {
sum += transform(element)
}
return sum
}
/**
* Returns the sum of all values produced by `transform` function from elements in the collection
*/
public inline fun BooleanArray.sumByDouble(transform: (Boolean) -> Double): Double {
var sum: Double = 0.0
for (element in this) {
sum += transform(element)
}
return sum
}
/**
* Returns the sum of all values produced by `transform` function from elements in the collection
*/
public inline fun ByteArray.sumByDouble(transform: (Byte) -> Double): Double {
var sum: Double = 0.0
for (element in this) {
sum += transform(element)
}
return sum
}
/**
* Returns the sum of all values produced by `transform` function from elements in the collection
*/
public inline fun CharArray.sumByDouble(transform: (Char) -> Double): Double {
var sum: Double = 0.0
for (element in this) {
sum += transform(element)
}
return sum
}
/**
* Returns the sum of all values produced by `transform` function from elements in the collection
*/
public inline fun DoubleArray.sumByDouble(transform: (Double) -> Double): Double {
var sum: Double = 0.0
for (element in this) {
sum += transform(element)
}
return sum
}
/**
* Returns the sum of all values produced by `transform` function from elements in the collection
*/
public inline fun FloatArray.sumByDouble(transform: (Float) -> Double): Double {
var sum: Double = 0.0
for (element in this) {
sum += transform(element)
}
return sum
}
/**
* Returns the sum of all values produced by `transform` function from elements in the collection
*/
public inline fun IntArray.sumByDouble(transform: (Int) -> Double): Double {
var sum: Double = 0.0
for (element in this) {
sum += transform(element)
}
return sum
}
/**
* Returns the sum of all values produced by `transform` function from elements in the collection
*/
public inline fun LongArray.sumByDouble(transform: (Long) -> Double): Double {
var sum: Double = 0.0
for (element in this) {
sum += transform(element)
}
return sum
}
/**
* Returns the sum of all values produced by `transform` function from elements in the collection
*/
public inline fun ShortArray.sumByDouble(transform: (Short) -> Double): Double {
var sum: Double = 0.0
for (element in this) {
sum += transform(element)
}
return sum
}
/**
* Returns the sum of all values produced by `transform` function from elements in the collection
*/
public inline fun <T> Iterable<T>.sumByDouble(transform: (T) -> Double): Double {
var sum: Double = 0.0
for (element in this) {
sum += transform(element)
}
return sum
}
/**
* Returns the sum of all values produced by `transform` function from elements in the collection
*/
public inline fun <T> Stream<T>.sumByDouble(transform: (T) -> Double): Double {
var sum: Double = 0.0
for (element in this) {
sum += transform(element)
}
return sum
}
/**
* Returns the sum of all values produced by `transform` function from elements in the collection
*/
public inline fun String.sumByDouble(transform: (Char) -> Double): Double {
var sum: Double = 0.0
for (element in this) {
sum += transform(element)
}
return sum
}