I am trying to implement a generic method to compute the mean of any kind of sequence (for example: List, Array) which contains any kind of numeric values (Int, Float, Double...), like this:
def mean[T <: Numeric[T]](data:Seq[T])(implicit number: Numeric[T]): T = {
data.foldLeft(number.zero)(number.plus) / data.size
}
However, the division operation cannot be resolved. That is because the Numeric type does not have this operation defined (from the ScalaDoc).
I want to convert it to double before proceeding with the division, but the method toDouble(x:T)
from Numeric type expects a param.
I have seen there is a type member for the Numeric[T] called NumericOps
that does implement the toDouble
method without receiving any param. Could I call this method.. somehow?