scala

Java SortedMap to Scala TreeMap

I'm having trouble converting a java SortedMap into a scala TreeMap. The SortedMap comes from deserialization and needs to be converted into a scala structure before being used. Some background, for the curious, is that the serialized structure is written through XStream and on desializing I register a converter that says anything that...

Scala: How to get class of mixin composition?

scala> import java.util.Properties import java.util.Properties scala> trait Foo extends Properties defined trait Foo scala> classOf[Foo] res0: java.lang.Class[Foo] = interface Foo scala> class FooP extends Foo defined class FooP scala> classOf[FooP] res1: java.lang.Class[FooP] = class FooP scala> classOf[Properties with Foo] <consol...

Scala objects not changing their internal state

I am seeing a problem with some Scala 2.7.7 code I'm working on, that should not happen if it the equivalent was written in Java. Loosely, the code goes creates a bunch of card players and assigns them to tables. class Player(val playerNumber : Int) class Table (val tableNumber : Int) { var players : List[Player] = List() def...

Scala foreach strange behaviour

I want to iterate over a list of values using a beautiful one-liner in Scala. For example, this one works well: scala> val x = List(1,2,3,4) x: List[Int] = List(1, 2, 3, 4) scala> x foreach println 1 2 3 4 But if I use the placeholder _, it gives me an error: scala> x foreach println(_ + 1) <console>:6: error: missing parameter typ...

Liftweb Menu customization

I want to create a menu that looks like: HOME | FOO | BAR | ABOUT | CONTACT How might I go about doing this? Here is what I have tried: <lift:Menu.builder ul:class="menu" li_item:class="current" /> and ul.menu li { display: inline; list-style-type: none; text-transform: uppercase; border-right: 1px solid white; padd...

Finding elements in a scala list and also know which predicate has been satisfied

I have the following problem in scala. I have to find the first element in al list which satisfies a predicate function with two conditions in OR. The problem is that I would like to get the element but also know which of the two conditions has been satisfied. Here is a simple example: val l1 = List("A", "B", "AA", "BB") val l2 = List("...

Implement/instantiate abstract class via reflection in Scala

I'm working on a framework for a EA (evolutionary alg) project in Scala. In this i have a trait that implements common EA-code and leaves problem spesific code like genotype convertion and fitness testing to classes that implement this trait. However, I don't want to fully implement the trait before it is actually run because of testing ...

Experiences with escape analysis enabled on the JVM

I've just tried the -XX:+DoEscapeAnalysis option enabled on a jdk6-u18 VM (on solaris) and had a rather disappointing experience. I'm running a scala application which has rather a lot of actors (20,000 of them). This is a recipe for garbage-creation! Typically the app can run with 256Mb of heap but generates huge amounts of garbage. In...

Strange GC behaviour of Scala actors application

I have an application which uses rather a lot of actors: 25,000 to be precise. It uses Scala 2.7.7 and is running on jdk6_u18. It basically listens to and processes market data and has very little state. It starts at 8.02am every day and within the hour it has crashed with an OutOfMemoryError. "Aha" you say, "you have a memory leak!" Ex...

Substitute values in a string with placeholders in Scala

I have just started using Scala and wish to better understand the functional approach to problem solving. I have pairs of strings the first has placeholders for parameter and it's pair has the values to substitute. e.g. "select col1 from tab1 where id > $1 and name like $2" "parameters: $1 = '250', $2 = 'some%'" There may be many more t...

Referring to the type of an inner class in Scala

The following code tries to mimic Polymorphic Embedding of DSLs: rather than giving the behavior in Inner, it is encoded in the useInner method of its enclosing class. I added the enclosing method so that user has only to keep a reference to Inner instances, but can always get their enclosing instance. By doing this, all Inner instances ...

Are there any good Clojure benchmarks?

Edit: The Clojure benchmarks are up on the Benchmarks Game. I have made this question community wiki and invite others to keep it updated. Is anyone aware of benchmarks of Clojure's performance? I have done some of my own (although nothing too formal) and it didn't fair too well in comparison to other functional languages (tried...

Concurrent Processing in Scala

Hello: I am trying use Concurrent programming in Scala. Based in this example here in StackOverflow, I made a program based on Problem 1 of Project Euler. I try three methods: The first one is a simple execution, with no paralelism. The second uses java.util.concurrency API through Executors and Callables. The third, based on page men...

In Scala, is there a way to take convert two lists into a Map?

I have a two lists, a List[A] and a List[B]. What I want is a Map[A,B] but I want the semantics of zip. So started out like so: var tuplesOfAB = listOfA zip listOfB Now I'm not sure how to construct a Map from my tuplesOfAB. As a follow-up question, I also want to invert my map so that from a Map[A,B] I can create a Map[B,A]. Can an...

In Scala, how to use Ordering[T] with List.min or List.max and keep code readable

In Scala 2.8, I had a need to call List.min and provide my own compare function to get the value based on the second element of a Tuple2. I had to write this kind of code: val list = ("a", 5) :: ("b", 3) :: ("c", 2) :: Nil list.min( new Ordering[Tuple2[String,Int]] { def compare(x:Tuple2[String,Int],y:Tuple2[String,Int]): Int = x._2...

Existential Quantification over Values

I came across existential quantification over values in the Scala Language Specification (3.2.10 Existential Types). x: y.Inner forSome{val y : Outer} Does someone have illustrative use cases for it? T forSome {val x: S} is defined as T forSome { type t <: S with Singleton }. The Singletron trait is mentioned in the Specification (3....

How do I find out Apache Buildr/Maven 2 repo names

I'm just starting to use Apache Buildr and I'm constantly running into the problem of not knowing what repo urls and versions are available for me to use. For example I want to use Scala 2.8 in a build file, the id i previously used was: 2.8.0-SNAPSHOT But now this is not found. I also want to use the latest version of Apache POI. If...

Many-value map in Scala

In Scala 2.8, I have an immutable map with multiple values for each key: Map[T,Iterable[U]] Is there a superior representation? Secondly, how would you generate such a map from Iterable[(T,U)] ? I am presently using: def toGroupedMap[T,U](vals: Iterable[(T,U)]): Map[T,Iterable[U]] = vals.groupBy(_._1).map({ case (s,it) => (s,it....

Strange behavior: Scala Actors 2.7.7 vs. 2.8-Snapshot

I'm an 18 years old trainee and I'm discovering scala which I like very much :-). To get familiar with the scala actors I wrote a small simulation with some gears and one controller. The ActorApplication creates N gears with random speed. The controller calculates a synchronization speed and starts the gear-actors. The gears synchronize ...

Scala XML Building: Adding children to existing Nodes

I Have an XML Node that I want to add children to over time: val root: Node = <model></model> But I cannot see methods such as addChild(), as I would like to write something along the lines of: def addToModel() = { root.addChild(<subsection>content</subsection>) } So after a single call to this method the root xml would be: <m...