Skip to content

Instantly share code, notes, and snippets.

@paulp
Created November 9, 2008 19:33
Show Gist options
  • Save paulp/23325 to your computer and use it in GitHub Desktop.
Save paulp/23325 to your computer and use it in GitHub Desktop.
object b {
trait Ops[T] {
def +(y: T): T
def ^(y: Int): T
}
implicit def intops(x: Int) = new Ops[Int] {
def +(y: Int) = x + y
def ^(y: Int) = Math.pow(x, y).toInt
}
implicit def floatops(x: Float) = new Ops[Float] {
def +(y: Float) = x + y
def ^(y: Int) = Math.pow(x, y).toFloat
}
def go[T <% Ops[T]](xs: Array[T]) = xs.map(_ ^ 2).reduceLeft(_ + _)
}
// scala> import b._
// import b._
//
// scala> go(Array(1,2,3))
// res0: Int = 14
//
// scala> go(Array(1f, 2.5f, 3.6f))
// res1: Float = 20.21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment