Guest User

Untitled

a guest
Oct 26th, 2013
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.39 KB | None | 0 0
  1. scala> implicit def tupleIsNumeric[A : Numeric, B : Numeric]: Numeric[(A, B)] = new Numeric[(A, B)] {
  2.      |   import Numeric.Implicits._
  3.      |
  4.      |   val na = implicitly[Numeric[A]]
  5.      |   val nb = implicitly[Numeric[B]]
  6.      |
  7.      |   def compare(x: (A, B), y: (A, B)): Int = na.compare(x._1, y._1) match {
  8.      |     case 0 => nb.compare(x._2, y._2)
  9.      |     case r => r
  10.      |   }
  11.      |   def fromInt(i: Int): (A, B) = na.fromInt(i) -> nb.fromInt(i)
  12.      |   def minus(x: (A, B), y: (A, B)): (A, B) = (x._1 - y._1, x._2 - y._2)
  13.      |   def negate(x: (A, B)): (A, B) = na.negate(x._1) -> nb.negate(x._2)
  14.      |   def plus(x: (A, B), y: (A, B)): (A, B) = (x._1 + y._1, x._2 + y._2)
  15.      |   def times(x: (A, B), y: (A, B)): (A, B) = na.times(x._1, y._1) -> nb.times(x._2, y._2)
  16.      |   def toDouble(x: (A, B)): Double = ???
  17.      |   def toFloat(x: (A, B)): Float = ???
  18.      |   def toInt(x: (A, B)): Int = ???
  19.      |   def toLong(x: (A, B)): Long = ???
  20.      | }
  21. tupleIsNumeric: [A, B](implicit evidence$1: scala.math.Numeric[A], implicit evidence$2: scala.math.Numeric[B])scala.math.Numeric[(A, B)]
  22.  
  23. scala> (1, 1.1) - (2, 2.2)
  24. res3: (Int, Double) = (-1,-1.1)
  25.  
  26. scala> (1, 1.1) + (2, 2.2)
  27. <console>:29: error: value + is not a member of (Int, Double)
  28.               (1, 1.1) + (2, 2.2)
  29.                        ^
  30.  
  31. scala> (1, 1.1) * (2, 2.2)
  32. res5: (Int, Double) = (2,2.4200000000000004)
Advertisement
Add Comment
Please, Sign In to add comment