I would like to generalize the Vector class to add some basic math features. I have done this for Vector[Scalar] (based on my Scalar class), a portion of which is shown below. How can I generalize this to also work for Vector[Int] and Vector[Double] without repeating the whole implementation? I am using Scala 3. Thanks.
implicit class Vectorx(self: Vector[Scalar]):
// adds basic math operations to the Vector[Scalar] class
import self._
def apply(i: Int): Scalar = self(i)
def unary_- = map(-_)
protected def zip(v: Vector[Scalar]) = { check(v); self zip v }
def + (v: Vector[Scalar]) = zip(v).map(t => t._1 + t._2)
def - (v: Vector[Scalar]) = zip(v).map(t => t._1 - t._2)
def * (s: Scalar) = map(_ * s)
def / (s: Scalar) = map(_ / s)
def % (s: Scalar) = map(_ % s)
// more