Created
November 9, 2008 19:33
-
-
Save paulp/23325 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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