本文整理汇总了Scala中scala.math.Numeric类的典型用法代码示例。如果您正苦于以下问题:Scala Numeric类的具体用法?Scala Numeric怎么用?Scala Numeric使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Numeric类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: Scorer
//设置package包名称以及导入依赖的类
package turksem.util
import scala.collection.mutable
import scala.math.Numeric
class Scorer[A, N] private (private[this] val map: mutable.Map[A, N])(implicit N: Numeric[N]) {
@inline def apply(a: A): N = get(a)
def get(a: A): N = map.get(a).getOrElse(N.zero)
def increment(a: A): Unit = add(a, N.one)
def add(a: A, n: N): Unit = map.put(a, N.plus(get(a), n))
def addAll(other: Iterator[(A, N)]): Unit =
other.foreach(Function.tupled(add))
def iterator: Iterator[(A, N)] = map.iterator
def keyIterator: Iterator[A] = map.keys.iterator
def isEmpty: Boolean = map.isEmpty
def nonEmpty: Boolean = map.nonEmpty
def size: Int = map.size
def max: N = if(map.isEmpty) N.zero else map.values.max
def min: N = if(map.isEmpty) N.zero else map.values.min
def sum: N = map.values.sum
def mean: Double = N.toDouble(sum) / size.toDouble
def median: Double = {
if(map.isEmpty) {
0.0
} else {
val sorted = iterator.map(_._2).toVector.sorted
if(sorted.size % 2 == 0) {
N.toDouble(N.plus(sorted(sorted.size / 2 - 1), sorted(sorted.size / 2))) / 2.0
} else {
N.toDouble(sorted(sorted.size / 2))
}
}
}
def sizeIf(p: A => Boolean): Int = map.keys.filter(p).size
def sumIf(p: A => Boolean): N = map.filter(x => p(x._1)).map(_._2).sum
}
object Scorer {
def apply[A, N : Numeric](): Scorer[A, N] = {
new Scorer(mutable.Map.empty[A, N])
}
def apply[A, N : Numeric](items: TraversableOnce[A]): Scorer[A, N] = {
val map = mutable.Map.empty[A, N]
val counter = new Scorer(map)
items.foreach(i => counter.increment(i))
counter
}
}