Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- scala> implicit def tupleIsNumeric[A : Numeric, B : Numeric]: Numeric[(A, B)] = new Numeric[(A, B)] {
- | import Numeric.Implicits._
- |
- | val na = implicitly[Numeric[A]]
- | val nb = implicitly[Numeric[B]]
- |
- | def compare(x: (A, B), y: (A, B)): Int = na.compare(x._1, y._1) match {
- | case 0 => nb.compare(x._2, y._2)
- | case r => r
- | }
- | def fromInt(i: Int): (A, B) = na.fromInt(i) -> nb.fromInt(i)
- | def minus(x: (A, B), y: (A, B)): (A, B) = (x._1 - y._1, x._2 - y._2)
- | def negate(x: (A, B)): (A, B) = na.negate(x._1) -> nb.negate(x._2)
- | def plus(x: (A, B), y: (A, B)): (A, B) = (x._1 + y._1, x._2 + y._2)
- | def times(x: (A, B), y: (A, B)): (A, B) = na.times(x._1, y._1) -> nb.times(x._2, y._2)
- | def toDouble(x: (A, B)): Double = ???
- | def toFloat(x: (A, B)): Float = ???
- | def toInt(x: (A, B)): Int = ???
- | def toLong(x: (A, B)): Long = ???
- | }
- tupleIsNumeric: [A, B](implicit evidence$1: scala.math.Numeric[A], implicit evidence$2: scala.math.Numeric[B])scala.math.Numeric[(A, B)]
- scala> (1, 1.1) - (2, 2.2)
- res3: (Int, Double) = (-1,-1.1)
- scala> (1, 1.1) + (2, 2.2)
- <console>:29: error: value + is not a member of (Int, Double)
- (1, 1.1) + (2, 2.2)
- ^
- scala> (1, 1.1) * (2, 2.2)
- res5: (Int, Double) = (2,2.4200000000000004)
Advertisement
Add Comment
Please, Sign In to add comment