I'd like to adapt this code as given here : Find min and max elements of array
def minMax(a: Array[Int]) : (Int, Int) = {
if (a.isEmpty) throw new java.lang.UnsupportedOperationException("array is empty")
a.foldLeft((a(0), a(0)))
{ case ((min, max), e) => (math.min(min, e), math.max(max, e))}
}
To work also with Long
, Float
and Double
(since these are the types accepted by scala.math.min/max. I tried:
def getMinAndMax[@specialized(Int,Long,Float,Double) T](x: Seq[T]) : (T, T) = {
if (x.isEmpty) throw new java.lang.UnsupportedOperationException("seq is empty")
x.foldLeft((x.head, x.head))
{ case ((min, max), e) => (math.min(min, e), math.max(max, e))}
}
But this does not compile either. Any advice?