standard library: 'sum' function added (KT-3714, KT-3843, KT-3126)

This commit is contained in:
nik
2013-11-24 17:33:05 +04:00
committed by Mikhael Bogdanov
parent 4ecfab6e0b
commit cde36adbdc
14 changed files with 209 additions and 17 deletions
+42
View File
@@ -408,3 +408,45 @@ public inline fun <T> Array<out T>.withIndices() : Iterator<Pair<Int, T>> {
return IndexIterator(iterator())
}
/**
* Sums up the elements
*/
public fun Array<Byte>.sum() : Int {
return fold(0, {a,b -> a+b})
}
/**
* Sums up the elements
*/
public fun Array<Short>.sum() : Int {
return fold(0, {a,b -> a+b})
}
/**
* Sums up the elements
*/
public fun Array<Int>.sum() : Int {
return fold(0, {a,b -> a+b})
}
/**
* Sums up the elements
*/
public fun Array<Long>.sum() : Long {
return fold(0.toLong(), {a,b -> a+b})
}
/**
* Sums up the elements
*/
public fun Array<Float>.sum() : Float {
return fold(0.toFloat(), {a,b -> a+b})
}
/**
* Sums up the elements
*/
public fun Array<Double>.sum() : Double {
return fold(0.0, {a,b -> a+b})
}
@@ -381,3 +381,10 @@ public inline fun ByteArray.withIndices() : Iterator<Pair<Int, Byte>> {
return IndexIterator(iterator())
}
/**
* Sums up the elements
*/
public fun ByteArray.sum() : Int {
return fold(0, {a,b -> a+b})
}
@@ -381,3 +381,10 @@ public inline fun DoubleArray.withIndices() : Iterator<Pair<Int, Double>> {
return IndexIterator(iterator())
}
/**
* Sums up the elements
*/
public fun DoubleArray.sum() : Double {
return fold(0.0, {a,b -> a+b})
}
@@ -381,3 +381,10 @@ public inline fun FloatArray.withIndices() : Iterator<Pair<Int, Float>> {
return IndexIterator(iterator())
}
/**
* Sums up the elements
*/
public fun FloatArray.sum() : Float {
return fold(0.toFloat(), {a,b -> a+b})
}
@@ -381,3 +381,10 @@ public inline fun IntArray.withIndices() : Iterator<Pair<Int, Int>> {
return IndexIterator(iterator())
}
/**
* Sums up the elements
*/
public fun IntArray.sum() : Int {
return fold(0, {a,b -> a+b})
}
@@ -376,3 +376,31 @@ public inline fun <T> Iterable<T>.withIndices() : Iterator<Pair<Int, T>> {
return IndexIterator(iterator())
}
/**
* Sums up the elements
*/
public fun Iterable<Int>.sum() : Int {
return fold(0, {a,b -> a+b})
}
/**
* Sums up the elements
*/
public fun Iterable<Long>.sum() : Long {
return fold(0.toLong(), {a,b -> a+b})
}
/**
* Sums up the elements
*/
public fun Iterable<Float>.sum() : Float {
return fold(0.toFloat(), {a,b -> a+b})
}
/**
* Sums up the elements
*/
public fun Iterable<Double>.sum() : Double {
return fold(0.0, {a,b -> a+b})
}
@@ -381,3 +381,10 @@ public inline fun LongArray.withIndices() : Iterator<Pair<Int, Long>> {
return IndexIterator(iterator())
}
/**
* Sums up the elements
*/
public fun LongArray.sum() : Long {
return fold(0.toLong(), {a,b -> a+b})
}
@@ -381,3 +381,10 @@ public inline fun ShortArray.withIndices() : Iterator<Pair<Int, Short>> {
return IndexIterator(iterator())
}
/**
* Sums up the elements
*/
public fun ShortArray.sum() : Int {
return fold(0, {a,b -> a+b})
}