Skip to content

Commit 4d4ebda

Browse files
committed
expanded sum with float, short, byte, biginteger and some warnings for conversions
1 parent ba00f0f commit 4d4ebda

File tree

1 file changed

+65
-3
lines changed
  • core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/math

1 file changed

+65
-3
lines changed

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/math/sum.kt

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.jetbrains.kotlinx.dataframe.math
22

33
import java.math.BigDecimal
4+
import java.math.BigInteger
45
import kotlin.reflect.KType
56

67
@PublishedApi
@@ -11,34 +12,77 @@ internal fun <T, R : Number> Iterable<T>.sumOf(type: KType, selector: (T) -> R?)
1112
}
1213
return when (type.classifier) {
1314
Double::class -> sumOf(selector as ((T) -> Double)) as R
15+
16+
// careful, conversion to Double to Float occurs! TODO
17+
Float::class -> sumOf { (selector as ((T) -> Float))(it).toDouble() }.toFloat() as R
18+
1419
Int::class -> sumOf(selector as ((T) -> Int)) as R
20+
21+
// careful, cast to Int occurs! TODO
22+
Short::class -> sumOf { (selector as ((T) -> Short))(it).toInt() } as R
23+
24+
// careful, cast to Int occurs! TODO
25+
Byte::class -> sumOf { (selector as ((T) -> Byte))(it).toInt() } as R
26+
1527
Long::class -> sumOf(selector as ((T) -> Long)) as R
28+
1629
BigDecimal::class -> sumOf(selector as ((T) -> BigDecimal)) as R
17-
else -> TODO()
30+
31+
BigInteger::class -> sumOf(selector as ((T) -> BigInteger)) as R
32+
33+
Number::class -> sumOf { (selector as ((T) -> Number))(it).toDouble() } as R
34+
35+
Nothing::class -> 0.0 as R
36+
37+
else -> throw IllegalArgumentException("sumOf is not supported for $type")
1838
}
1939
}
2040

2141
@PublishedApi
2242
internal fun <T : Number> Iterable<T>.sum(type: KType): T =
2343
when (type.classifier) {
2444
Double::class -> (this as Iterable<Double>).sum() as T
45+
2546
Float::class -> (this as Iterable<Float>).sum() as T
47+
48+
// careful, cast to Int occurs! TODO
2649
Int::class, Short::class, Byte::class -> (this as Iterable<Int>).sum() as T
50+
2751
Long::class -> (this as Iterable<Long>).sum() as T
52+
2853
BigDecimal::class -> (this as Iterable<BigDecimal>).sum() as T
29-
else -> throw IllegalArgumentException("Sum is not supported for $type")
54+
55+
BigInteger::class -> (this as Iterable<BigInteger>).sum() as T
56+
57+
Number::class -> (this as Iterable<Number>).map { it.toDouble() }.sum() as T
58+
59+
Nothing::class -> 0.0 as T
60+
61+
else -> throw IllegalArgumentException("sum is not supported for $type")
3062
}
3163

3264
@JvmName("sumNullableT")
3365
@PublishedApi
3466
internal fun <T : Number> Iterable<T?>.sum(type: KType): T =
3567
when (type.classifier) {
3668
Double::class -> (this as Iterable<Double?>).asSequence().filterNotNull().sum() as T
69+
3770
Float::class -> (this as Iterable<Float?>).asSequence().filterNotNull().sum() as T
71+
72+
// careful, cast to Int occurs! TODO
3873
Int::class, Short::class, Byte::class -> (this as Iterable<Int?>).asSequence().filterNotNull().sum() as T
74+
3975
Long::class -> (this as Iterable<Long?>).asSequence().filterNotNull().sum() as T
76+
4077
BigDecimal::class -> (this as Iterable<BigDecimal?>).asSequence().filterNotNull().sum() as T
41-
else -> TODO()
78+
79+
BigInteger::class -> (this as Iterable<BigInteger?>).asSequence().filterNotNull().sum() as T
80+
81+
Number::class -> (this as Iterable<Number?>).asSequence().filterNotNull().map { it.toDouble() }.sum() as T
82+
83+
Nothing::class -> 0.0 as T
84+
85+
else -> throw IllegalArgumentException("sum is not supported for $type")
4286
}
4387

4488
@PublishedApi
@@ -58,3 +102,21 @@ internal fun Sequence<BigDecimal>.sum(): BigDecimal {
58102
}
59103
return sum
60104
}
105+
106+
@PublishedApi
107+
internal fun Iterable<BigInteger>.sum(): BigInteger {
108+
var sum: BigInteger = BigInteger.ZERO
109+
for (element in this) {
110+
sum += element
111+
}
112+
return sum
113+
}
114+
115+
@PublishedApi
116+
internal fun Sequence<BigInteger>.sum(): BigInteger {
117+
var sum: BigInteger = BigInteger.ZERO
118+
for (element in this) {
119+
sum += element
120+
}
121+
return sum
122+
}

0 commit comments

Comments
 (0)