scala - 高效的 Scala 惯用方法来选择前 85% 的排序值?

标签 scala

给定一个非递增的数字列表,我想选择列表中前 85% 的值。这是我目前的做法。

scala> val a = Array(8.60, 6.85, 4.91, 3.45, 2.74, 2.06, 1.53, 0.35, 0.28, 0.12)
a: Array[Double] = Array(8.6, 6.85, 4.91, 3.45, 2.74, 2.06, 1.53, 0.35, 0.28, 0.12)

scala> val threshold = a.sum * 0.85
threshold: Double = 26.2565

scala> val successiveSums = a.tail.foldLeft(Array[Double](a.head)){ case (x,y) => x ++ Array(y + x.last) }
successiveSums: Array[Double] = Array(8.6, 15.45, 20.36, 23.81, 26.549999999999997, 28.609999999999996, 30.139999999999997, 30.49, 30.77, 30.89)

scala> successiveSums.takeWhile( x => x <= threshold )
res40: Array[Double] = Array(8.6, 15.45, 20.36, 23.81)

scala> val size = successiveSums.takeWhile( x => x <= threshold ).size
size: Int = 4

scala> a.take(size)
res41: Array[Double] = Array(8.6, 6.85, 4.91, 3.45)

我要改进它

  • 表现
  • 代码大小

有什么建议吗?

最佳答案

关于代码大小,考虑这个 oneliner,

a.take( a.scanLeft(0.0)(_+_).takeWhile( _ <= a.sum * 0.85 ).size - 1 )

这里 scanLeft 累加。

在性能方面,标记中间值可能有助于避免重新计算相同的操作,即

val threshold = a.sum * 0.85
val size = a.scanLeft(0.0)(_+_).takeWhile( _ <= threshold ).size - 1
a.take( size )

关于scala - 高效的 Scala 惯用方法来选择前 85% 的排序值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29340188/

相关文章:

scala - 如何从不同的模块 "include"Thrift 文件?

sql - 使用 Spark SQL 跳过/获取

scala - sbt 中的 mainClass 设置如何工作?

scala - 使用 View 时出现匹配错误

regex - Scala 中使用正则表达式仅提取第一个匹配项

scala - Play - 如何为 Dev/Prod 使用不同的配置文件?

方法中的 Scala 导入语句

scala - 不兼容的 Jackson 版本 : Spark Structured Streaming

scala - 让 sbt 在文件更改时重新运行 - `~ compile` 相当于 `run`

scala - scala中的类型删除和继承