Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Or else... what?

View gist:2f1e7c1d6b3cc7af3ebe
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
object Test {
val pf: PartialFunction[String, Boolean] = {
case "hello" => true
}
val slow: PartialFunction[String, Boolean] =
((pf /: List.tabulate(100){_ => pf}) {
(a,e) => a.orElse(e)
}).orElse {
case _ => false
}
def alot(block: => Any) {
for (_ <- 1 to 100000) block
}
def time(block: => Any) {
val start = System.currentTimeMillis
block
println(System.currentTimeMillis - start)
}
}
Owner

scala> Test.time { Test.alot { Test.pf("hello") } }
6

scala> Test.time { Test.alot { Test.slow("hello") } }
1115

scala> Test.time { Test.alot { Test.slow("hell") } }
1113

Owner

With the second version:

scala> Test.time { Test.alot { Test.pf("hello") } }
6

scala> Test.time { Test.alot { Test.slow("hello") } }
1254

scala> Test.time { Test.alot { Test.slow("hell") } }
108

Owner

scala> Test.slow("hello")
apply #-1
isDefinedAt #5
isDefinedAt #4
isDefinedAt #3
isDefinedAt #2
isDefinedAt #1
apply #5
isDefinedAt #4
isDefinedAt #3
isDefinedAt #2
isDefinedAt #1
apply #4
isDefinedAt #3
isDefinedAt #2
isDefinedAt #1
apply #3
isDefinedAt #2
isDefinedAt #1
apply #2
isDefinedAt #1
apply #1
res4: Boolean = true

Owner

scala> Test.slow("hell")
apply #-1
isDefinedAt #5
isDefinedAt #4
isDefinedAt #3
isDefinedAt #2
isDefinedAt #1
res3: Boolean = false

Owner

I think I can improve on this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.