Created
March 4, 2015 18:02
-
-
Save gregghz/d6056874c4228271335d 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
package main | |
object Main { | |
trait Thing[A] { | |
def f(a: Int): A | |
} | |
def g[A: Thing](i: Int): A = implicitly[Thing[A]].f(i) | |
def g[A](i: Int, f: Int => A): A = f(i) | |
case class Example(x: Int) | |
object Example { | |
implicit val thing = new Thing[Example] { | |
def f(a: Int): Example = Example(a / 2) | |
} | |
} | |
def time[A](f: => A): A = { | |
val t0 = System.nanoTime() | |
val result = f | |
val t1 = System.nanoTime() | |
println("Elapsed time: " + (t1 - t0)) | |
result | |
} | |
def main(args: Array[String]): Unit = { | |
val max = Int.MaxValue | |
val seq = 0 until max | |
def f(a: Int): Example = Example(a / 2) | |
for (i <- seq) { | |
i * 2 | |
} | |
// explicit | |
time { | |
for (i <- seq) { | |
g(i, x => Example(x / 2)) | |
} | |
} | |
// explicit (no anon) | |
// time { | |
// for (i <- seq) { | |
// g(i, f) | |
// } | |
// } | |
// implicit | |
// time { | |
// for (i <- seq) { | |
// g[Example](i) | |
// } | |
// } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment