scala

What are the precise rules for when you can omit parenthesis, dots, braces, = (functions) etc?

What are the precise rules for when you can omit (omit) parentheses, dots, braces, = (functions) etc? For example (service.findAllPresentations.get.first.votes.size) must be equalTo(2) service is my object def findAllPresentations:Option[List[Presentation]] votes returns List[Vote] must and be are both functions of specs Why can't I...

"eval" in Scala

Can Scala be used to script a Java application? I need to load a piece of Scala code from Java, set up an execution scope for it (data exposed by the host application), evaluate it and retrieve a result object from it. The Scala documentation shows how easy it is to call compiled Scala code from Java (because it gets turned into to re...

Why can't Scala infer the type parameter in this example?

Suppose I have two classes, Input and Output, which are designed to be connected to each other. Output produces values of some type, and Input consumes them. class Input[T] { var output: Option[Output[_ <: T]] = None } class Output[T] { var input: Option[Input[_ >: T]] = None } It's okay if an Input and Output pair don't operate o...

problem extending scala Actor

I m new to scala. When learning Actor, I tried to extend it to save one line of def: import scala.actors.Actor import Actor._ class Actoo(actoo: =>Unit) extends Actor { def act() {actoo} } object run extends Application { /* // this one runs well val a = new Actor { def act() { receive { case 1 => pr...

Scala factorial on large numbers sometimes crashes and sometimes doesn't

The following program, was compiled and tested, it sometimes return the result, and sometimes fills the screen with java.lang.StackOverflowError at scala.BigInt$.apply(BigInt.scala:47) at scala.BigInt.equals(BigInt.scala:129) at scala.runtime.BoxesRunTime.equals(Unknown Source) at bigint$.factorial(fact2.scala:3) at bigint$.factorial(fa...

Using alternative comparison in HashSet

I stumbled across this problem when creating a HashSet[Array[Byte]] to use in a kind of HatTrie. Apparently, the standard equals() method on arrays checks for identity. How can I provide the HashSet with an alternative Comparator that uses .deepEquals() for checking if an element is contained in the set? Basically, I want this test to...

Using Either to process failures in Scala code

Option monad is a great expressive way to deal with something-or-nothing things in Scala. But what if one needs to log a message when "nothing" occurs? According to the Scala API documentation, The Either type is often used as an alternative to scala.Option where Left represents failure (by convention) and Right is akin to Some...

Which Java Web-Framework is better suited to be used together with Scala?

Hi! I have a personal project - a web mashup which uses multiple REST-Apis and Webservices, which I would like to program using scala, mostly because I would like to use it as a pet project for learning this language. However, it is possible that the project grows and that other java programmers contribute to it. Furthermore, I would li...

Idiomatic table cell renderers in Scala

I had been using the traditional Java TableCellRenderer approach for providing the renderers in a scala.swing.Table where I declare my renderers on the table's TableColumnModel. The code for this looked like: val myTable = new Table { lazy val tcm = initColumnModel peer.setColumnModel(tcm) override protected def rendererCompon...

How can I share memory between two JVM instances?

I build a huge graph in JVM (Scala) which I want to use repeatedly, tweaking algorithms. I'd rather not reload it each time from disk. Is there a way to have it sit in one JVM while connecting from another, where the algorithms are being developed? ...

Looking for a generic Oauth library for scala, java or python

I have a few already but wanted to merge them into one public domain oauth code library for twitter, facebook, friendfeed (and let other developers improve the library for their preferred connections). I'm having a tough time just debugging mashing friendfeed and twitters oauth into one friendly python program running on the Google App ...

Scala best way of turning a Collection into a Map-by-key? (2nd variant)

(This is a variant to this Q&A) Say I have this: List( "foo", "bar", "spam" ) I want to create a Map for which the key is the length of the String and the value is a Collection of all the Strings that have that length. In other words, given the about List, we'd get: Map( 3 -> List(foo, bar), 4 -> List(spam) ) The code I've written...

Scala enumeration and reflection

Hello all, After working in Java for a long time, I started to get interested in Scala. As a learning project, I am trying to duplicate a java library that stores and retrieves state objects from the database. For this, I would like to be able to just specify a state object like this: @PersistName("PERSON") case class Person extends ...

Scala: keyword as package name

I'm trying to use a Java library (no source code available) which defines some xxx.xxx.object package. Scala complains about the presence of "object" in the package name, so I can't import from it, and I can't refer to its classes with fully qualified name either. Is there a way around it? ...

Avoiding Scala memory leaks - Scala constructors

Hi! I was working through the "Programming in Scala" book, and was struck by a bit of a problem in the implementation of the class Rational in Chapter 6. This is my initial version of the Rational class (based on the book) class Rational(numerator: Int, denominator: Int) { require(denominator != 0) private val g = gcd(numerator.a...

Lift RewriteResponse not finding a valid url

Hi I'm having some trouble with Lift and URL rewriting. I've written a simple rewrite rule: LiftRules.rewrite.append { case RewriteRequest( ParsePath(List("user", userID), _, _, _), _, _) => { println(userID) RewriteResponse(List("viewUser"), Map("userID" -> urlDecode(userID))) } } So when I enter http://loca...

Using JDOQL results in Scala

I'm trying to use a JDO with Google App Engine and Scala. The api for the execute returns Object (but it's really a java collection) and I want to get it into a scala list to iterate over it. My code looks like this so far: val pm = PMF.factory.getPersistenceManager val query = "select from User " val gamelist:List[User] = List(pm.ne...

Scala, animations and graphical user interface

Hi, I want to be able to do graphics with Scala programming language. Need to be able to do animations and attractive user interface, where to start? ...

How does Scala's apply() method magic work?

In Scala, if I define a method called apply in a class or a top-level object, that method will be called whenever I append a pair a parentheses to an instance of that class, and put the appropriate arguments for apply() in between them. e.g., class Foo(x: Int) { def apply(y: Int) = { x*x + y*y } } val f = new Foo(3) f(4) // ...

Sleeping actors?

What's the best way to have an actor sleep? I have actors set up as agents which want to maintain different parts of a database (including getting data from external sources). For a number of reasons (including not overloading the database or communications and general load issues), I want the actors to sleep between each operation. I'm ...