【发布时间】:2013-03-11 08:25:25
【问题描述】:
我开始学习 Scala,在为不耐烦的人阅读 Scala 时,对其中一个练习找到了以下解决方案:
//No function
def positivesThenZerosAndNegatives(values: Array[Int]) = {
Array.concat(for (value <- values if value > 0) yield value,
for (value <- values if value == 0) yield value,
for (value <- values if value < 0) yield value)
}
但现在我试图将在每个综合应用上应用过滤器的函数作为参数传递:
//Trying to use a function (filter)
def positivesThenZerosAndNegatives2(values: Array[Int]) = {
Array.concat(filter(values, _ > 0), filter(values, _ == 0), filter(values, _ < 0))
}
def filter[T: Int](values: Array[T], f: (T) => Boolean) = {
for (value <- values if f(value)) yield value
}
我还没有找到引用元素数组的正确方法。
【问题讨论】:
标签: scala functional-programming