org.nield.kotlinstatistics.CategoricalStatistics.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-statistics Show documentation
Show all versions of kotlin-statistics Show documentation
Statistical and analytical extensions for Kotlin
package org.nield.kotlinstatistics
fun Sequence.mode() = countBy()
.entries
.asSequence()
.sortedByDescending { it.value }
.toList().let { list ->
list.asSequence()
.takeWhile { list[0].value == it.value }
.map { it.key }
}
fun Iterable.mode() = asSequence().mode()
fun Array.mode() = asIterable().mode()
fun ByteArray.mode() = asIterable().mode()
fun ShortArray.mode() = asIterable().mode()
fun IntArray.mode() = asIterable().mode()
fun LongArray.mode() = asIterable().mode()
fun FloatArray.mode() = asIterable().mode()
fun DoubleArray.mode() = asIterable().mode()
//AGGREGATION OPERATORS
/**
* Groups each distinct value with the number counts it appeared
*/
fun Sequence.countBy() = groupApply({ it }, {it.count()})
/**
* Groups each distinct value with the number counts it appeared
*/
fun Iterable.countBy() = asSequence().countBy()
/**
* Groups each distinct key with the number counts it appeared
*/
inline fun Sequence.countBy(crossinline keySelector: (T) -> K) = groupApply(keySelector) { it.count() }
/**
* Groups each distinct key with the number counts it appeared
*/
inline fun Iterable.countBy(crossinline keySelector: (T) -> K) = asSequence().countBy(keySelector)