1
我開始學習斯卡拉並在閱讀斯卡拉的不耐煩了,到了以下解決方案的一個練習:函數獲取陣列和功能的元件陣列
//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)
}
但現在我試圖通過爲PARAM應用於過濾器上的每一個全面的功能:
//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
}
我還沒有找到引用的元件陣列的正確途徑。
我不知道我想要的過濾器已經存在。謝謝! – 2013-03-11 20:34:20