-
-
Save n8han/2f1e7c1d6b3cc7af3ebe to your computer and use it in GitHub Desktop.
Or else... what?
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 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) | |
} | |
} |
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!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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