I'm starting to learn Scala and while reading Scala for the Impatient, got to the following solution to one of the exercises:
//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)
}
But now I was trying to pass as param the function that applies the filter on each comprehensive for:
//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
}
I haven't found the right way to refer to an element array.