scala

Scala vs. Groovy vs. Clojure

Can someone please explain the major differences between Scala, Groovy and Clojure. I know each of these compiles to run on the JVM but I'd like a simple comparison between them. ...

How to reduce Scala (/ Java) startup overhead?

I'm pretty ignorant of the Java world (I do mostly C / Python) but Scala looked interesting enough to pull me in. One problem I'm having with it is the enormous startup overhead - 0.3 seconds minimum, much more if I'm using the interpreter instead of compiling, compared to effectively 0 for Python or C. So even though the language is ten...

Is there potential starvation in this code or is it just me?

I am trying to learn Scala and find it a great language so far. I learn from "Beginning Scala" by David Pollak. In chapter 3 there is this piece of code, which illustrates how to write multi-threaded code without synchronized blocks (this code is copied from the book, it's available for download from Apress site, I don't mean to break an...

Calculating the Moving Average of a List

This weekend I decided to try my hand at some Scala and Clojure. I'm proficient with object oriented programming, and so Scala was easy to pick up as a language, but wanted to try out functional programming. This is where it got hard. I just can't seem to get my head into a mode of writing functions. As a expect functional programm...

Why is appending to a list bad?

I've recently started learning scala, and I've come across the :: (cons) function, which prepends to a list. In the book "Programming in Scala" it states that there is no append function because appending to a list has performance o(n) whereas prepending has a performance of o(1) Something just strikes me as wrong about that statement. ...

Scala doesn't have enums - what to use instead of an enum

Scala doesn't have type-safe enums like Java has. If I have a set of related constants then what is the best way in Scala to represent those constants? ...

Alternatives to multimethods in Scala or Jython

I often face the problem of wanting to add additional methods to classes I don't control. For instance, I might want to have a function prettyPrint that can operate on different object types that do not have a unified api (e.g. special __str__ methods). The Nice language and R accomplishes this using multimethods, which avoid the Visit...

Practical examples of using symbols in Scala?

Scala has symbols - names that start with a single quote ' and which are a kind of string constants. I know symbols from Ruby (where they start with a colon). In Ruby they are used for some meta-programming tasks, like generating getters and setters for member variables (for example attr_reader :name to generate a getter for name). I h...

Table Cell renderer using Nimbus and Scala

I asked this question about a problem I was seing with a cell renderer using the Nimbus look and feel and the issue has turned out to be possibly to do with Scala. Basically I have a cell renderer which extends Panel (as opposed to DefaultTableCellRenderer) and it is behaving oddly: it is not rendering the alternate row colors properly w...

Common programming mistakes for Scala developers to avoid

In the spirit of Common programming mistakes for Java developers to avoid? Common programming mistakes for JavaScript developers to avoid? Common programming mistakes for .NET developers to avoid? Common programming mistakes for Haskell developers to avoid? Common programming mistakes for Python developers to avoid? Common Programm...

How to determine if a list is subset of another list?

What is efficient way to determine if a list is a subset of another list? Example: is_subset(List(1,2,3,4),List(2,3)) //Returns true is_subset(List(1,2,3,4),List(3,4,5)) //Returns false I am mostly looking for efficient algorithm and not too concern how the list is stored. It can be stored in array, link list or other data struct...

Avoiding implicit def ambiguity in Scala

I am trying to create an implicit conversion from any type (say, Int) to a String... An implicit conversion to String means RichString methods (like reverse) are not available. implicit def intToString(i: Int) = String.valueOf(i) 100.toCharArray // => Array[Char] = Array(1, 0, 0) 100.reverse // => error: value reverse is not a member ...

Scala BigDecimal division

What use is the division operator on a scala BigDecimal? val d1 = BigDecimal(2) val d2 = BigDecimal(3) val div = d1 / d2 //throws ArithmeticException: non-terminating decimal expansion In order to get this to work, you need to define a DECIMAL128 context on the decimals. Unfortunately the only way I can see of doing this is: val div...

Scala profiler?

Hi, I started to program in Scala recently. I'm looking for a free Scala profiler. Reading from the language's official site led me to YourKit, but the program was not a free one. Googling "scala profiler" didnt give me any relevant result. So how do I profile my program written in Scala? I prefer a graphical plugin to Netbeans or Ecli...

How can I force the type of an array when initialized in Scala?

Basically, I have an array like this: val base_length = Array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 0 ); And when scala sees it, it wants to do this: base_length: Array[Int] = Array(...) But I would prefer for it to do this: base_length: Array[Byte] =...

Does Scala's BigDecimal violate the equals/hashCode contract?

As the Ordered trait demands, the equals method on Scala's BigDecimal class is consistent with the ordering. However, the hashcode is simply taken from the wrapped java.math.BigDecimal and is therefore inconsistent with equals. object DecTest { def main(args: Array[String]) { val d1 = BigDecimal("2") val d2 = BigDecimal("2.00"...

What are the benefits of Scala?

I am a Java developer and I want to know how I can use Scala in my Java programs? ...

Can a range be matched in Scala?

Is it possible to match a range of values in Scala? For example: val t = 5 val m = t match { 0 until 10 => true _ => false } m would be true if t was between 0 and 10, but false otherwise. This little bit doesn't work of course, but is there any way to achieve something like it? ...

Object converting string into "A"

I would like to write a class looking like this: class Store[+A](dest: Symbol)(implicit c: String => A) extends Action(dest) { override def update(options: HashMap[Symbol,Any], arg: String): Unit = { options += ((dest -> c(arg))) } } object Store { def apply[A](dest: Symbol)(c: String=>A) = new Store[A](dest)(c) def apply[A...

Scala Parser Issues

Hi I am having issues testing out the Scala Parser Combinator functionality for a simple Book DSL. Firstly there is a book class: case class Book (name:String,isbn:String) { def getNiceName():String = name+" : "+isbn } Next, there is the simple parser: object BookParser extends StandardTokenParsers { lexical.reserved += ("book","...