Skip to content

Commit dc4687d

Browse files
committed
hid public Iterable.mean() overloads; There's .average() in stdlib, plus, aside from columns/dfs, creating public mean functions just clutters the public scope. Simplified internal mean logic and function overloads.
1 parent 6e028f1 commit dc4687d

File tree

2 files changed

+41
-20
lines changed

2 files changed

+41
-20
lines changed

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

+27-18
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,38 @@
11
package org.jetbrains.kotlinx.dataframe.math
22

3+
import org.jetbrains.kotlinx.dataframe.api.mean
34
import org.jetbrains.kotlinx.dataframe.api.skipNA_default
45
import org.jetbrains.kotlinx.dataframe.impl.renderType
6+
import org.jetbrains.kotlinx.dataframe.util.INTERNAL_MEAN
7+
import org.jetbrains.kotlinx.dataframe.util.MEAN
8+
import org.jetbrains.kotlinx.dataframe.util.SEQUENCE_FLOAT_MEAN
59
import java.math.BigDecimal
610
import java.math.BigInteger
711
import kotlin.reflect.KType
812
import kotlin.reflect.full.withNullability
13+
import kotlin.reflect.typeOf
14+
15+
@JvmName("meanIterableReified")
16+
@PublishedApi
17+
internal inline fun <reified T : Number> Iterable<T>.mean(skipNA: Boolean = skipNA_default): Double =
18+
mean(typeOf<T>(), skipNA)
919

1020
@PublishedApi
1121
internal fun <T : Number> Iterable<T>.mean(type: KType, skipNA: Boolean = skipNA_default): Double =
1222
asSequence().mean(type, skipNA)
1323

24+
@JvmName("meanSequenceReified")
25+
internal inline fun <reified T : Number> Sequence<T>.mean(skipNA: Boolean = skipNA_default): Double =
26+
mean(typeOf<T>(), skipNA)
27+
1428
internal fun <T : Number> Sequence<T>.mean(type: KType, skipNA: Boolean = skipNA_default): Double {
1529
if (type.isMarkedNullable) {
1630
return filterNotNull().mean(type.withNullability(false), skipNA)
1731
}
1832
return when (type.classifier) {
1933
Double::class -> (this as Sequence<Double>).mean(skipNA)
2034

21-
Float::class -> (this as Sequence<Float>).mean(skipNA)
35+
Float::class -> (this as Sequence<Float>).map { it.toDouble() }.mean(skipNA)
2236

2337
Int::class -> (this as Sequence<Int>).map { it.toDouble() }.mean(false)
2438

@@ -42,7 +56,7 @@ internal fun <T : Number> Sequence<T>.mean(type: KType, skipNA: Boolean = skipNA
4256
}
4357
}
4458

45-
public fun Sequence<Double>.mean(skipNA: Boolean = skipNA_default): Double {
59+
private fun Sequence<Double>.mean(skipNA: Boolean = skipNA_default): Double {
4660
var count = 0
4761
var sum: Double = 0.toDouble()
4862
for (element in this) {
@@ -59,8 +73,9 @@ public fun Sequence<Double>.mean(skipNA: Boolean = skipNA_default): Double {
5973
return if (count > 0) sum / count else Double.NaN
6074
}
6175

76+
@Deprecated(SEQUENCE_FLOAT_MEAN, level = DeprecationLevel.ERROR)
6277
@JvmName("meanFloat")
63-
public fun Sequence<Float>.mean(skipNA: Boolean = skipNA_default): Double {
78+
internal fun Sequence<Float>.mean(skipNA: Boolean = skipNA_default): Double {
6479
var count = 0
6580
var sum: Double = 0.toDouble()
6681
for (element in this) {
@@ -77,12 +92,15 @@ public fun Sequence<Float>.mean(skipNA: Boolean = skipNA_default): Double {
7792
return if (count > 0) sum / count else Double.NaN
7893
}
7994

95+
@Deprecated(INTERNAL_MEAN, level = DeprecationLevel.HIDDEN)
8096
@JvmName("doubleMean")
81-
public fun Iterable<Double>.mean(skipNA: Boolean = skipNA_default): Double = asSequence().mean(skipNA)
97+
internal fun Iterable<Double>.mean(skipNA: Boolean = skipNA_default): Double = mean(typeOf<Double>(), skipNA)
8298

99+
@Deprecated(INTERNAL_MEAN, level = DeprecationLevel.HIDDEN)
83100
@JvmName("floatMean")
84-
public fun Iterable<Float>.mean(skipNA: Boolean = skipNA_default): Double = asSequence().mean(skipNA)
101+
internal fun Iterable<Float>.mean(skipNA: Boolean = skipNA_default): Double = mean(typeOf<Float>(), skipNA)
85102

103+
@Deprecated(MEAN, level = DeprecationLevel.HIDDEN)
86104
@JvmName("intMean")
87105
public fun Iterable<Int>.mean(): Double =
88106
if (this is Collection) {
@@ -96,6 +114,7 @@ public fun Iterable<Int>.mean(): Double =
96114
if (count > 0) sum / count else Double.NaN
97115
}
98116

117+
@Deprecated(MEAN, level = DeprecationLevel.HIDDEN)
99118
@JvmName("shortMean")
100119
public fun Iterable<Short>.mean(): Double =
101120
if (this is Collection) {
@@ -109,6 +128,7 @@ public fun Iterable<Short>.mean(): Double =
109128
if (count > 0) sum / count else Double.NaN
110129
}
111130

131+
@Deprecated(MEAN, level = DeprecationLevel.HIDDEN)
112132
@JvmName("byteMean")
113133
public fun Iterable<Byte>.mean(): Double =
114134
if (this is Collection) {
@@ -122,6 +142,7 @@ public fun Iterable<Byte>.mean(): Double =
122142
if (count > 0) sum / count else Double.NaN
123143
}
124144

145+
@Deprecated(MEAN, level = DeprecationLevel.HIDDEN)
125146
@JvmName("longMean")
126147
public fun Iterable<Long>.mean(): Double =
127148
if (this is Collection) {
@@ -135,19 +156,7 @@ public fun Iterable<Long>.mean(): Double =
135156
if (count > 0) sum / count else Double.NaN
136157
}
137158

138-
@JvmName("bigIntegerMean")
139-
public fun Iterable<BigInteger>.mean(): Double =
140-
if (this is Collection) {
141-
if (size > 0) sumOf { it.toDouble() } / size else Double.NaN
142-
} else {
143-
var count = 0
144-
val sum = sumOf {
145-
count++
146-
it.toDouble()
147-
}
148-
if (count > 0) sum / count else Double.NaN
149-
}
150-
159+
@Deprecated(MEAN, level = DeprecationLevel.HIDDEN)
151160
@JvmName("bigDecimalMean")
152161
public fun Iterable<BigDecimal>.mean(): Double =
153162
if (this is Collection) {

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/util/deprecationMessages.kt

+14-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ package org.jetbrains.kotlinx.dataframe.util
55
* After each release, all messages should be reviewed and updated.
66
* Level.WARNING -> Level.ERROR
77
* Level.ERROR -> Remove
8+
*
9+
* Level.HIDDEN can remain as is but needs to be removed together with other deprecations in
10+
* the same cycle.
811
*/
912

1013
// region WARNING in 0.15, ERROR in 0.16
1114

12-
private const val MESSAGE_0_16 = "Will be removed in 0.16."
15+
private const val MESSAGE_0_16 = "Will be ERROR in 0.16."
1316

1417
internal const val DF_READ_NO_CSV = "This function is deprecated and should be replaced with `readCSV`. $MESSAGE_0_16"
1518
internal const val DF_READ_NO_CSV_REPLACE =
@@ -44,11 +47,20 @@ internal const val PARSER_OPTIONS = "This constructor is only here for binary co
4447

4548
internal const val PARSER_OPTIONS_COPY = "This function is only here for binary compatibility. $MESSAGE_0_16"
4649

50+
internal const val SEQUENCE_FLOAT_MEAN =
51+
"`Sequence<Float>.mean()` is removed since it's already covered by other overloads. $MESSAGE_0_16"
52+
53+
internal const val INTERNAL_MEAN =
54+
"`Iterable.mean(skipNA)` is removed since it's already covered by other overloads. $MESSAGE_0_16"
55+
56+
internal const val MEAN =
57+
"`Iterable.mean()` is removed from the public API because it's outside the scope of DataFrame. You can still call `.mean()` on a column. For most types there's already `.average()` in stdlib. $MESSAGE_0_16"
58+
4759
// endregion
4860

4961
// region WARNING in 0.16, ERROR in 0.17
5062

51-
private const val MESSAGE_0_17 = "Will be removed in 0.17."
63+
private const val MESSAGE_0_17 = "Will be ERROR in 0.17."
5264

5365
// endregion
5466

0 commit comments

Comments
 (0)