secret
Created

Or else... what?

  • Download Gist
gistfile1.scala
Scala
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)
}
}

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

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

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

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

I think I can improve on this!

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.