It seems easy problem for any specific kind of Number i.e. Double/Integer but it is hard to write in general case.
implicit def iterebleWithAvg(data:Iterable[Double]) = new {
def avg:Double = data.sum / data.size
}
How to implement this for any kind of Number(Int,Float,Double,BigDecemial)?
You have to pass an implicit
Numericwhich will allow summation and conversion to Double:The compiler will provide the correct instance for you:
You can the use the function to define an implicit view (provided you propagate the implicit numeric dependency):