Skip to content

Instantly share code, notes, and snippets.

@aguestuser
Last active August 29, 2015 14:16
Show Gist options
  • Save aguestuser/c45fe10cea8abb4ed0f4 to your computer and use it in GitHub Desktop.
Save aguestuser/c45fe10cea8abb4ed0f4 to your computer and use it in GitHub Desktop.
type mystery
//Riddle me this...
//This compiles:
def plotGrowth[A,T[B] <: Seq[B]](l: T[A], fn: T[A] => T[A]): Boolean = {
val intervals = (0 to 50).map(_ * l.size / 50)
intervals map { i =>
val sub = l.take(i)
System.gc()
val start = System.nanoTime
val elapsed = (System.nanoTime - start)
println(s"$i, $elapsed") }
true }
// As does this:
def plotGrowthA[A](l: ArrayBuffer[A], fn: ArrayBuffer[A] => ArrayBuffer[A]): Boolean = {
val intervals = (0 to 50).map(_ * l.size / 50)
intervals map { i =>
val sub = l.take(i)
val elapsed = time(sub,fn)
println(s"$i, $elapsed") }
true }
def plotGrowthL[A](l: List[A], fn: List[A] => List[A]): Boolean = {
val intervals = (0 to 50).map(_ * l.size / 50)
intervals map { i =>
val sub = l.take(i)
val elapsed = time(sub,fn)
println(s"$i, $elapsed") }
true }
def time[A,T[B] <: Seq[B]](l: T[A], fn: T[A] => T[A]): Long = {
System.gc()
val start = System.nanoTime
fn(l)
(System.nanoTime - start) / 1000 /*to millis*/ }
// But this does not:
def plotGrowthDumb[A,T[B] <: Seq[B]](l: T[A], fn: T[A] => T[A]): Boolean = {
val intervals = (0 to 50).map(_ * l.size / 50)
intervals map { i =>
val sub = l.take(i)
val elapsed = time(sub,fn)
println(s"$i, $elapsed") }
true }
def time[A,T[B] <: Seq[B]](l: T[A], fn: T[A] => T[A]): Long = {
System.gc()
val start = System.nanoTime
fn(l)
(System.nanoTime - start) / 1000 /*to millis*/ }
// call to `time` on line 45 complains about the argument `fn`
// says it expects a function of type `Seq[A] => Seq[A]`
// but got one of type `T[A] => T[A]`
// anyone got any ideas?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment