Reputation: 11
I have a little question about this piece of code (which does not compile) :
def avg(seq: Seq[Int]): Double = seq.sum.toDouble/seq.length
def avg(seq: Seq[Double]): Double = seq.sum/seq.length
the idea is simple as shown above, the first method calculates the average to a sequence of integers, and the second does the same to a seq of Doubles
but it gives me a compilation error around repetition, I want to know why...and if you have a much Scala approach than this code to perform the same idea please, share it in your answer Thank you
Upvotes: 0
Views: 74
Reputation: 51271
The problem is due to type erasure, which can be overcome by using the Numeric
type class.
def avg[N:Numeric](ns :Seq[N]) :Double =
implicitly[Numeric[N]].toDouble(ns.sum) / ns.length
avg(List(3, 5, 7, 11)) //res0: Double = 6.5
avg(Vector(9.2, 3.3, 1.7, 21.1)) //res1: Double = 8.825
You can also place it in an implicit class for a more concise expression.
implicit class GetAvg[N:Numeric](ns :Seq[N]) {
def avg :Double = implicitly[Numeric[N]].toDouble(ns.sum) / ns.length
}
Stream(3, 5, 7, 11).avg //res2: Double = 6.5
Seq(9.2, 3.3, 1.7, 21.1).avg //res3: Double = 8.825
Upvotes: 3