All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nield.kotlinstatistics.CategoricalStatistics.kt Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
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)





© 2015 - 2024 Weber Informatics LLC | Privacy Policy