scala

Is there an analog in Scala for the Rails "returning" method?

In Rails, one could use: returning Person.create do |p| p.first_name = "Collin" p.last_name = "VanDyck" end Avoiding having to do this: person = Person.create person.first_name = "Collin" person.last_name = "VanDyck" person I think the former way is cleaner and less repetitive. I find myself creating this method in my Scala pr...

Summary/reference documentation on Scala standard library types

Details on the packages/types is in the Scala API documentation on scala-lang.org. But that's organised by class and I (as a Scala neophyte) find it difficult to locate the exact data type I need and work out what operation are supported on what (especially in the huge and powerful scala.collections.* tree). Is there an online or dead-t...

scala list of objects, using groupBy with average

Hi, basically I'm not really a Java/Scala fan, but unfortunately I'm forced to use it for my studies. Anyways, I was given an assignment: What the program gets is a list of objects like: Mark(val name String, val style_mark Int, val other_mark Int). How can I use groupBy, to group the marks by name, and get an average for style_mark an...

Is this surprising (to me) difference in Map's behaviour in Scala 2.8.0 and 2.7.7 expected?

It looks like -- in Scala 2.8.0 -- if you map() a Map instance to a sequence of 2-tuples that you end up getting a Map back. When this happens, any of the 2-tuples with the same first element are considered duplicates, and you only end up getting the last one. This is different from what happened in 2.7.7. This is easier to understand...

error: not found: value Auctioneer (LiftActor)

Greetings I have the following class package org.developerworks.comet import net.liftweb.http._ import net.liftweb.common.Full import net.liftweb.http.S._ import net.liftweb.http.SHtml._ import net.liftweb.http.js.JsCmd import net.liftweb.http.js.JsCmds._ import net.liftweb.http.js.JE._ import net.liftweb.util.Helpers._ import net.lif...

Can I define a nameless method in a Scala class?

Is this possible to reach? If yes, please correct my Foo declaration syntax. class Foo (...) { ... def /* the nameless method name implied here */ (...) : Bar = new Bar (...) ... } class Bar (...) { ... } val foo : Foo = new Foo (...) val fooBar : Bar = foo (...) ...

scala sort a list by object parameter error

Ok, I wouldn't be coming to You for help if I knew what to do, anyways, still having problems with my "program". class Mark(val name: String, val style_mark: Double, val other_mark: Double) {} object Test extends Application { val m1 = new Mark("Smith", 18, 16); val m2 = new Mark("Cole", 14, 7); val m3 = new Mark("James", 13, 1...

Can I define a class with no public constructor and place a factory method for this class objects in a different class in Scala?

For example (maybe a bit clumsy from a real life view, but just to illustrate): "User" is a case class containing user name and id. Id can be never set manually, and a User class instance with no id set has no sense. A UserBase class maintains users base and has a "getUser (name : String) : User" method returning a consistent User inst...

Scala Swing component sizing

Scala is an awesome language, but unfortunately the library documentation is lacking. How do I change the initial size of a component? I have nothing on it (intentionally), but would like it to be a certain size anyway. I currently have ... contents = new BoxPanel(Orientation.Vertical) { contents += new BoxPanel(Orientation.Horizont...

Scala compiler says my method is recursive in case when implicits and anonymous class is used

I want to be able to write code like 10 times { doSomething } So I thought I could do that with implicits. When i execute the following code in the Scala REPL it gets defined correctly scala> implicit def intToMyRichInt(count: Int) = { | new { | def times(f: => Unit) = { | 1 to count foreach { _ => f...

Is it considered a good style to use whitespace to align the code?

For example, what piece of code is considered better styled? If I show my code to a professional developer and ask if my code is good or not, will using the second style be probably considered as a (minor, but...) minus or a plus to my code quality? I myself tend to like the second style but would prefer to comply to the most common opi...

How to write a class destructor in Scala?

I'd like to have a FileWriter opened during the whole time a class instance exists. So I need to close it in a destructor. But how to specify a destructor in Scala? ...

Why is using replicate much slower than serial execution?

Hi, I've got a bit of a problem. I wanted to use scala.concurrent.ops.replicate to parallelize my program. But I found out, that the algorithm actually becomes much slower. So I wrote a little test and still got the same result. So here they are. Serial Code: Takes about 63 seconds to finish object SerTest { def main(args: Array[Stri...

How do I write a regex that matches all characters that are not a '$' followed by 'i' or '{'?

Meaning, I want to match: $10 or $ but not this: ${name} or: $image{http://wrfgadgadga.com/gadgad.png} I also want to match everything else... normal characters, symbols, numbers, etc. Matching everything but things that start with $ is easy. It's like this: def literalCharacter: Parser[String] = """[^\$]""".r I've tried ...

Scala: divide strings

Hi folks, The title sounds a bit crazy but having the * function for java.lang.String in Scala ("a" * 3 = "aaa"), why don't we have a / function so that "aaa" / "a" = 3 ? Cheers Parsa ...

Explain this pattern matching code

This code is from this article: object & { def unapply[A](a: A) = Some(a, a) } "Julie" match { case Brothers(_) & Sisters(_) => "Julie has both brother(s) and sister(s)" case Siblings(_) => "Julie's siblings are all the same sex" case _ => "Julie has no siblings" } // => "Julie has both brother(s) and sister(s)" I am unable to...

Does scala affect AspectJ/Spring AOP at all? I'm having a thread locking problem when running tests when an @Configurable is being called

I have a strange problem where when I run my tests using maven, it locks whenever a test method calls into an object with an annotation @Configurable. I can run the tests fine in IDEA using the AspectJ Weaver plugin (remarkably), but I cannot do it with maven (whether I execute them in IDEA or just in a terminal). Even weirder, if I pre...

Scala map example not working as expected

Am doing all the examples from the "Pragmatic Bookshelf Programming Scala" book. It is simple singleton example but am not getting it right i.e. values from map are not fetched. Can you point out the error. class Marker(val color: String) { println("Creating " + this) override def toString(): String = "marker color is " + color } ...

restart iterator on exceptions in Scala

I have an iterator (actually a Source.getLines) that's reading an infinite stream of data from a URL. Occasionally the iterator throws a java.io.IOException when there is a connection problem. In such situations, I need to re-connect and re-start the iterator. I want this to be seamless so that the iterator just looks like a normal itera...

How to prevent a ResultSet from being invalidated on Connection close?

I'd like to pass out a result set from a function that executes a query and closes the connection. But the ResultSet gets invalidated as soon as its parent Connection is closed and throws "java.sql.SQLException: Operation not allowed after ResultSet closed". How to avoid this? ...