diff --git a/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/math/mean.kt b/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/math/mean.kt index d2c924c6f..4ce9e68f9 100644 --- a/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/math/mean.kt +++ b/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/math/mean.kt @@ -1,16 +1,30 @@ package org.jetbrains.kotlinx.dataframe.math +import org.jetbrains.kotlinx.dataframe.api.mean import org.jetbrains.kotlinx.dataframe.api.skipNA_default import org.jetbrains.kotlinx.dataframe.impl.renderType +import org.jetbrains.kotlinx.dataframe.util.INTERNAL_MEAN +import org.jetbrains.kotlinx.dataframe.util.MEAN +import org.jetbrains.kotlinx.dataframe.util.SEQUENCE_FLOAT_MEAN import java.math.BigDecimal import java.math.BigInteger import kotlin.reflect.KType import kotlin.reflect.full.withNullability +import kotlin.reflect.typeOf + +@JvmName("meanIterableReified") +@PublishedApi +internal inline fun Iterable.mean(skipNA: Boolean = skipNA_default): Double = + mean(typeOf(), skipNA) @PublishedApi internal fun Iterable.mean(type: KType, skipNA: Boolean = skipNA_default): Double = asSequence().mean(type, skipNA) +@JvmName("meanSequenceReified") +internal inline fun Sequence.mean(skipNA: Boolean = skipNA_default): Double = + mean(typeOf(), skipNA) + internal fun Sequence.mean(type: KType, skipNA: Boolean = skipNA_default): Double { if (type.isMarkedNullable) { return filterNotNull().mean(type.withNullability(false), skipNA) @@ -18,7 +32,7 @@ internal fun Sequence.mean(type: KType, skipNA: Boolean = skipNA return when (type.classifier) { Double::class -> (this as Sequence).mean(skipNA) - Float::class -> (this as Sequence).mean(skipNA) + Float::class -> (this as Sequence).map { it.toDouble() }.mean(skipNA) Int::class -> (this as Sequence).map { it.toDouble() }.mean(false) @@ -42,7 +56,7 @@ internal fun Sequence.mean(type: KType, skipNA: Boolean = skipNA } } -public fun Sequence.mean(skipNA: Boolean = skipNA_default): Double { +private fun Sequence.mean(skipNA: Boolean = skipNA_default): Double { var count = 0 var sum: Double = 0.toDouble() for (element in this) { @@ -59,8 +73,9 @@ public fun Sequence.mean(skipNA: Boolean = skipNA_default): Double { return if (count > 0) sum / count else Double.NaN } +@Deprecated(SEQUENCE_FLOAT_MEAN, level = DeprecationLevel.ERROR) @JvmName("meanFloat") -public fun Sequence.mean(skipNA: Boolean = skipNA_default): Double { +internal fun Sequence.mean(skipNA: Boolean = skipNA_default): Double { var count = 0 var sum: Double = 0.toDouble() for (element in this) { @@ -77,12 +92,15 @@ public fun Sequence.mean(skipNA: Boolean = skipNA_default): Double { return if (count > 0) sum / count else Double.NaN } +@Deprecated(INTERNAL_MEAN, level = DeprecationLevel.HIDDEN) @JvmName("doubleMean") -public fun Iterable.mean(skipNA: Boolean = skipNA_default): Double = asSequence().mean(skipNA) +internal fun Iterable.mean(skipNA: Boolean = skipNA_default): Double = mean(typeOf(), skipNA) +@Deprecated(INTERNAL_MEAN, level = DeprecationLevel.HIDDEN) @JvmName("floatMean") -public fun Iterable.mean(skipNA: Boolean = skipNA_default): Double = asSequence().mean(skipNA) +internal fun Iterable.mean(skipNA: Boolean = skipNA_default): Double = mean(typeOf(), skipNA) +@Deprecated(MEAN, level = DeprecationLevel.HIDDEN) @JvmName("intMean") public fun Iterable.mean(): Double = if (this is Collection) { @@ -96,6 +114,7 @@ public fun Iterable.mean(): Double = if (count > 0) sum / count else Double.NaN } +@Deprecated(MEAN, level = DeprecationLevel.HIDDEN) @JvmName("shortMean") public fun Iterable.mean(): Double = if (this is Collection) { @@ -109,6 +128,7 @@ public fun Iterable.mean(): Double = if (count > 0) sum / count else Double.NaN } +@Deprecated(MEAN, level = DeprecationLevel.HIDDEN) @JvmName("byteMean") public fun Iterable.mean(): Double = if (this is Collection) { @@ -122,6 +142,7 @@ public fun Iterable.mean(): Double = if (count > 0) sum / count else Double.NaN } +@Deprecated(MEAN, level = DeprecationLevel.HIDDEN) @JvmName("longMean") public fun Iterable.mean(): Double = if (this is Collection) { @@ -135,19 +156,7 @@ public fun Iterable.mean(): Double = if (count > 0) sum / count else Double.NaN } -@JvmName("bigIntegerMean") -public fun Iterable.mean(): Double = - if (this is Collection) { - if (size > 0) sumOf { it.toDouble() } / size else Double.NaN - } else { - var count = 0 - val sum = sumOf { - count++ - it.toDouble() - } - if (count > 0) sum / count else Double.NaN - } - +@Deprecated(MEAN, level = DeprecationLevel.HIDDEN) @JvmName("bigDecimalMean") public fun Iterable.mean(): Double = if (this is Collection) { diff --git a/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/util/deprecationMessages.kt b/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/util/deprecationMessages.kt index b25e7fb60..f819b0f9c 100644 --- a/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/util/deprecationMessages.kt +++ b/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/util/deprecationMessages.kt @@ -5,11 +5,14 @@ package org.jetbrains.kotlinx.dataframe.util * After each release, all messages should be reviewed and updated. * Level.WARNING -> Level.ERROR * Level.ERROR -> Remove + * + * Level.HIDDEN can remain as is but needs to be removed together with other deprecations in + * the same cycle. */ // region WARNING in 0.15, ERROR in 0.16 -private const val MESSAGE_0_16 = "Will be removed in 0.16." +private const val MESSAGE_0_16 = "Will be ERROR in 0.16." internal const val DF_READ_NO_CSV = "This function is deprecated and should be replaced with `readCSV`. $MESSAGE_0_16" 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 internal const val PARSER_OPTIONS_COPY = "This function is only here for binary compatibility. $MESSAGE_0_16" +internal const val SEQUENCE_FLOAT_MEAN = + "`Sequence.mean()` is removed since it's already covered by other overloads. $MESSAGE_0_16" + +internal const val INTERNAL_MEAN = + "`Iterable.mean(skipNA)` is removed since it's already covered by other overloads. $MESSAGE_0_16" + +internal const val MEAN = + "`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" + // endregion // region WARNING in 0.16, ERROR in 0.17 -private const val MESSAGE_0_17 = "Will be removed in 0.17." +private const val MESSAGE_0_17 = "Will be ERROR in 0.17." // endregion