Skip to content

Instantly share code, notes, and snippets.

@gregghz
Created March 4, 2015 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregghz/d6056874c4228271335d to your computer and use it in GitHub Desktop.
Save gregghz/d6056874c4228271335d to your computer and use it in GitHub Desktop.
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