【发布时间】:2021-03-02 13:59:46
【问题描述】:
下面是我用来过滤列表数据的滑动窗口的代码,并且数据的每个元素都匹配一个谓词,然后过滤数据:
val data : Map[String, List[Double]] = Map(
"a" -> List(0.086, -0.398, -0.398, -0.312, 0.312, 0.312, 0.312, 0.312, 0.312, 0.312),
"b" -> List(-0.119, -0.119, 1.007, 1.201, 1.201, 1.201, -1.201, 1.201, -1.201, -1.201)
)
def getChanges(f : Double => Boolean, data : Map[String, List[Double]], windowSize : Int) = {
val result = data.map {
case (key, value) => key -> value.sliding(windowSize).filter(_.forall(f)).toList
}.filter {
case (_, values) => values.nonEmpty
}
result
}
val threshold = 0
def f(percentChange : Double) = {
percentChange > threshold
}
getChanges(f , data , 2).foreach(println)
打印:
(a,List(List(0.312, 0.312), List(0.312, 0.312), List(0.312, 0.312), List(0.312, 0.312), List(0.312, 0.312)))
(b,List(List(1.007, 1.201), List(1.201, 1.201), List(1.201, 1.201)))
这按预期工作,但有没有更惯用的方式来声明和调用函数 f ?
阈值设置在函数f 之外,这似乎是一种不好的编码习惯,因为在f 之外声明了阈值,但我不确定是否存在任何替代方案。另外我可能想介绍更复杂的过滤器逻辑,例如:
val upperThreshold = 0
val lowerThreshold = -5
def f(percentChange : Double) = {
percentChange > lowerThreshold and percentChange <= upperThreshold
}
但同样,也许有更好的方法来实现这一点。
【问题讨论】:
-
我不确定问题是什么?如果不想在函数内部定义阈值,那就去做吧,有什么问题?
-
这个问题和你的previous有什么不同?
标签: scala