Skip to content

Instantly share code, notes, and snippets.

@n8han
Created November 19, 2011 17:48
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 n8han/2f1e7c1d6b3cc7af3ebe to your computer and use it in GitHub Desktop.
Save n8han/2f1e7c1d6b3cc7af3ebe to your computer and use it in GitHub Desktop.
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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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