Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Or else... what?
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)
}
}
@n8han
Owner
n8han commented Nov 19, 2011

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

@n8han
Owner
n8han commented Nov 19, 2011

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

@n8han
Owner
n8han commented Nov 19, 2011

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

@n8han
Owner
n8han commented Nov 19, 2011

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

@n8han
Owner
n8han commented Nov 19, 2011

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.