I have written a function to accept the below type of values: (1, Array(1.0,2.0,3.0)) That is a Tuple with Int being first value and next being an Array of Doubles.
I would also like it to accept an Array of Integers as well. The function I have written is as below:
def getCountsAndAverages[T](Parameter: Tuple2[Int, Array[T]])(implicit n:Numeric[T]) = {
(Parameter._1, (Parameter._2.size, Parameter._2.map(n.toDouble).sum/Parameter._2.size))
}
The tuple's first parameter is a number and then its an Array of number of times it appears in the file.
It is working fine for sample cases, however I am reading a text file which has the data in the same format as needed for this function to work. I am calling this function using 'map' operation as:
parsedfile.map(getCountsAndAverages)
I am getting the below errors:
◾could not find implicit value for parameter n: Numeric[T]
◾not enough arguments for method getCountsAndAverages: (implicit n: Numeric[T])(Int, (Int, Double)). Unspecified value parameter n.
I would be grateful for any help or suggestions
You can use any one of the following syntax
def foo[T](x: T)(implicit n: Numeric[T]) = n.toDouble(x)
or
def foo[T : Numeric](x: T) = implicitly[Numeric[T]].toDouble(x)
In your case
def getCountsAndAverages[T: Numeric](Parameter: Tuple2[Int, Array[T]]) = {
(Parameter._1, (Parameter._2.size, Parameter._2.map(implicitly[Numeric[T]].toDouble(_)).sum / Parameter._2.size))
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With