Why does a small change to this Scala code make such a huge difference to performance?

I’m running on a 32-bit Debian 6.0 (Squeeze) system (a 2.5 GHz Core 2 CPU), sun-java6 6.24-1 but with the Scala 2.8.1 packages from Wheezy. This code, compiled with scalac -optimise, takes over 30 seconds to run: object Performance { import scala.annotation.tailrec @tailrec def gcd(x:Int,y:Int):Int = { if (x == 0) y else gcd(y%x,x) } val p … Read more

scala: How to pass an expanded list as varargs into a method?

When creating a Map in scala, I call Map(entities.map{e => e.id -> e}), and I get: found : scala.collection.mutable.IndexedSeq[(Int, Entity)] required: (Int, Entity) This is because the signature for Map.apply is: def apply[A, B](elems: (A, B)*): CC[A, B], which requires a varargs style argument. Is there a way to convert the IndexedSeq so that it … Read more

Why are Arrays invariant, but Lists covariant?

E.g. why does val list:List[Any] = List[Int](1,2,3) work, but val arr:Array[Any] = Array[Int](1,2,3) fails (because arrays are invariant). What is the desired effect behind this design decision? Answer Because it would break type-safety otherwise. If not, you would be able to do something like this: val arr:Array[Int] = Array[Int](1,2,3) val arr2:Array[Any] = arr arr2(0) = … Read more

Declare variable in a Play2 scala template

How do you declare and initialize a variable to be used locally in a Play2 Scala template? I have this: @var title : String = “Home” declared at the top of the template, but it gives me this error: illegal start of simple expression “””),_display_(Seq[Any](/*3.2*/var)),format.raw/*3.5*/(“”” title : String = “Home” Answer @defining(“foo”) { title=> <div>@title</div> … Read more

Automatically and Elegantly flatten DataFrame in Spark SQL

All, Is there an elegant and accepted way to flatten a Spark SQL table (Parquet) with columns that are of nested StructType For example If my schema is: foo |_bar |_baz x y z How do I select it into a flattened tabular form without resorting to manually running df.select(“foo.bar”,”foo.baz”,”x”,”y”,”z”) In other words, how do … Read more