scala

how do i create a map with a type parameter of class

I'm trying to do something like this: import scala.swing class componentMapper { val map = Map[Class[_], Componenet]() def apply(c: Class[_], component: Component) = map += (c -> componenet) } class Render extends ComponentMapper { def getRenderer(value: AnyRef) = map(value.getClass) } This doesn't seem to work. ...

What are stackable modifications?

I've been reading a book about Scala and there's mention of stackable modifications using traits. What are stackable modifications and for what purposes are they meant to be used? ...

What is the best way to format a string in Scala?

I was wondering what the best way to format a string would be in Scala. I'm reimplementing the toString method for a class, and it's a rather long and complex string. I thought about using String.format but it seems to have problems with Scala. Is there a native Scala function for doing this? ...

Mapping over sublists in Scala

I know that the map function takes each element of a list (a sequence) and applies a function to it. Recursively (and without respect to termination conditions, etc) map(s, f) = f(s.head) :: map(s.tail, f) I am looking for a function that does something like foo(s, f) = f(s) :: map(s.tail, f). So a 'mapper' where the mapping func...

What does a single apostrophe mean in Scala?

In this slide show on ScalaActors.pdf what does the single quote indicate when the message is sent to the pong actor? class Ping(count: int, pong: Pong) extends Actor { def act() { pong ! 'Ping // what does the single quote indicate??? receive { case 'Pong => } } } ...

How can I construct and parse a JSON string in Scala / Lift

I am using JsonResponse to send some JSON to the client. I would like to test that I am building the correct message for a given input, so it seemed natural to me to parse the resulting JSON and validate against a data structure rather than parsing a string by hand. Lift's JsObj produces a string which uses single quotes. This is vali...

Why is PartialFunction <: Function in Scala?

In Scala, the PartialFunction[A, B] class is derived from type Function[A, B] (see Scala Reference, 12.3.3). However, this seems counterintuitive to me, since a Function (which needs to be defined for all A) has more stringent requirements than a PartialFunction, which can be undefined at some places. The problem I've came accross was t...

Scala: How do I cast a variable?

Given a variable with type Graphics, I want to cast it to Graphics2D in Scala. ...

Scala: Two methods, different parameter types but same code: How to unify?

I have the following classes: case class Vec2(x: Int, y: Int) { def +(other: Vec2) = Vec2(x + other.x, y + other.y) } case class Vec3(x: Int, y: Int, z: Int) { def +(other: Vec3) = Vec3(x + other.x, y + other.y, z + other.z) } And the following methods: def doStuff1(a: Vec2, b: Vec2) = (a, a + b) def doStuff2(b: Vec3, b: Vec3) = (a, ...

How to check existence of a program in the path

I'm writing a program in scala which call: Runtime.getRuntime().exec( "svn ..." ) I want to check if "svn" is available from the commandline (ie. it is reachable in the PATH). How can I do this ? PS: My program is designed to be run on windows ...

Scala Swing event framework - where do I add my reactors?

I'm trying to catch a mouse-click even on a Table (which should cause a popup to be shown). The table is inside a ScrollPane which is (in turn) inside a Panel. I have added reactions to all the classes, but I can never seem to actually get a click event to be caught! class MyPanel extends GridBagPanel { val gbc = new GridBagContraints...

Using @Autowired of spring with scala

hi everyone, i am using spring from scala and i am facing a problem when trying to inject a service with a trait/superclass. This is my code: trait MyServiceHolder{ var myService:MyService = null @Autowired def setMyService(ms:MyService) = myService = ms } @RunWith(classOf[SpringJUnit4ClassRunner]) @ContextConfiguration(Array("...

Scala Popup Menu

How do I cause a popup to get shown in Scala? I have a "backdoor" but it seems pretty ugly to me: val item = new MenuIterm(new Action("Say Hello") { def apply = println("Hello World"); }) //SO FAR SO GOOD, NOW FOR THE UGLY BIT! val popup = new javax.swing.JPopupMenu popup.add(item.peer) popup.setVisible(true) ...

When to use the equals sign in a Scala method declaration?

With equals sign: object HelloWorld { def main(args: Array[String]) = { println("Hello!") } } Without equals sign: object HelloWorld { def main(args: Array[String]) { println("Hello!") } } Both of the above programs execute the same way. From here I read that when the equals sign is missing, the method will return U...

What does middleware mean for Twitter and Scala?

Reference another SO question I had, I was given this article about Twitter moving from Rails to Scala, and in the article is this comment: By the end of this year, Payne said, Twitter hopes to have its entire middleware infrastructure and its APIs ported to the new language. Ruby will remain, but only on the front end. "We...

Does Scala scale better then other JVM languages?

Here is the only way I know to ask it at the moment. As Understand it Scala uses the Java Virtual Machine. I thought Jruby did also. Twitter switched its middleware to Scala. Could they have done the same thing and used Jruby? Could they have started with Jruby to start with and not had their scaling problems that caused them to mo...

scala Map filterKeys: Projection cannot be assigned to a Map reference

The following code: var m: Map[String, Int] = Map("A" -> 1, "BB" -> 2, "CCC" -> 3) m = m filterKeys { s => s.length < 3 } Does not compile. I get the following error: error: type mismatch found: collection.this.Map.Projection[scala.this.Predef.String,scala.this.Int] required: collection.this.Map[scala.this.Predef.String,scala...

scala collection.Map cannot be added to

Why can't I add to a scala.collection.Map? It seems that this trait is pretty useless without this functionality. Couldn't they have overridden the ++ method in Iterable and reduced the return type to a Map? P.S. I don't mean that it should be mutable, just that it should be able to return a new Map with an added mapping (or mappings)...

Code Coverage Tools for Scala

What are the available code coverage tools for Scala? I have Scala spec tests and a Hudson continuous integration set-up. Is there something I can hook-in to this setup to measure and track code coverage? ...

Handling unions, subsets and supersets in Scala

I need to write a code snippet that would compare multiple arrays and produce the set that match the data in those arrays, produce the data set only in array A, but not in array B,C,D,in array B but not in A,C,D, being able to handle any number of arrays (i.e. dynamically looped). The code should utilize anonymous functions in Scala (i.e...