scala

Scala best way of turning a Collection into a Map-by-key?

If I have a collection c of type T and there is a property p on T (of type P, say), what is the best way to do a map-by-extracting-key? val c: Collection[T] val m: Map[P, T] One way is the following: m = new HashMap[P, T] c foreach { t => m add (t.getP, t) } But now I need a mutable map. Is there a better way of doing this so that ...

Converting Java collection into Scala collection

Related to this question, how do convert a Java collection (java.util.List say) into a scala collection List? EDIT; what I am actually trying to do is convert a Java API call to Spring's SimpleJdbcTemplate, which returns a java.util.List<T>, into a scala immutable HashSet. So for example: val l: java.util.List[String] = javaApi.query( ...

Scala collection standard practice

Coming from a Java background, I'm used to the common practice of dealing with collections: obviously there would be exceptions but usually code would look like: public class MyClass { private Set<String> mySet; public void init() { Set<String> s = new LinkedHashSet<String>(); s.add("Hello"); s.add("World"); mySet ...

Why is Scala's immutable Set not covariant in its type?

EDIT: Re-written this question based on original answer The scala.collection.immutable.Set class is not covariant in its type parameter. Why is this? import scala.collection.immutable._ def foo(s: Set[CharSequence]): Unit = { println(s) } def bar(): Unit = { val s: Set[String] = Set("Hello", "World"); foo(s); //DOES NOT CO...

Scala do I have to convert to a Seq when creating a collection from an iterable?

Perhaps I am barking up the wrong tree (again) but if it is normal practice to have a property typed as a scala.collection.immutable.Set[A], then how would you create one of these given a scala.Iterable[A]? For example: class ScalaClass { private var s: scala.collection.immutable.Set[String] def init(): Unit = { val i =...

Scala API design; a service which returns a Set<I> where I is some interface (abstract/trait)

I have asked quite a few questions about the Scala collection types and how they might actually be used. Consider how I might write some service API/implementation in Java: public interface JavaProductAPI { public Set<IProduct> getProducts(String isin); } And now the impl: public class JavaProductAPIImpl implements JavaProductAP...

Scala Map: mysterious syntactic sugar?

I have just found out this syntax for a scala Map (used here in mutable form) val m = scala.collection.mutable.Map[String, Int]() m("Hello") = 5 println(m) //PRINTS Map(Hello -> 5) Now I'm not sure whether this is syntactic sugar built in to the language, or whether something more fundamental is going on here involving the fact that a...

What is the _root_ package in Scala?

I'm using IntelliJ IDEA with the Scala plugin. If I reference HashMap in code, and then use Alt-Enter to add the import, the package gets imported as: _root_.scala.collection.immutable.HashMap What's the root part of this? It seems to work with and without it. ...

What's the Scala syntax for a function taking any subtype of Ordered[A]?

I want to write a function that works on any Scala type with a total ordering (i.e. I can use '<' on it). What's the syntax for that? The best I've come up with is def lessThan[T <: Ordered[T]](x: T, Y: T) = x < y That doesn't work, though, when I try using it from the REPL: scala> lessThan(1, 2) <console>:8: error: inferred type a...

Is CouchDB best suited for dynamic languages?

I´m in touch with CouchDB and the idea of mapping its results to Scala objects, as well as find some natural way to iteract with it, came immediatly. But I see that Dynamic languages such as Ruby and Javascript do things very well with the json/document-centric/shchema-free aproach of CouchDB. Any good aproach to do things with Couch i...

How do I implicitly convert Tuples to vector in Scala

I want to be able to implicitly convert Tuples of numbers (Ints and double) into a vector object. Assuming a Vector class with a + method case class Vector(x: Double, y:Double){ def + (v:Vector)= new Vector(x+v.x,y+v.y) } My goal is to have the following code work. val vec = (1,2)+(.5,.3) // vec == Vector(1.5,2.3) I can get it...

What are the key differences between Scala and Groovy?

On the surface Groovy and Scala look pretty similar, aside from Scala being statically typed, and Groovy dynamic. What are the other key differences, and advantages each have over the other? How similar are they really? Is there competition between the two? If so, who do you think will win in the long run? ...

Enterprise Scala

Are you using Scala to develop your enterprise grade applications? EDIT: agree, the word enterprise is not well defined. By asking this question I meant to ask whether you use Scala for real projects which are meant to be used not just for playing. Using Scala for a personal project which is meant to be used by more than just it's crea...

Web Application Development on Scala

Hi, I have just learnt scala and i want to try some web development with it. On Google, one of the frameworks i came across (the only one?) was Lift. Does anyone have any experience with it? Any suggestions for web application development with scala on any other frameworks? Thanks! ...

What's so great about Scala?

What makes Scala such a wonderful language, other than the type system? Almost everything I read about the language brings out 'strong typing' as a big reason to use Scala, but there has to be more than that. What are some of the other compelling and/or cool language features that make Scala a really useful tool? ...

How should I specify the type of JSON-like unstructured data in Scala?

I'm considering porting a very simple text-templating library to scala, mostly as an exercise in learning the language. The library is currently implemented in both Python and Javascript, and its basic operation more or less boils down to this (in python): template = CompiledTemplate('Text {spam} blah {eggs[1]}') data = { 'spam': 1, 'e...

How to add external library to the classpath in scala's interpreter?

I'm trying to reference some classes in the Scala Interpreter that were writen and compiled using Eclipse. Eclipse stores compiled Java byte code in a folder {workspace}/myProject/bin. Is there some command which will add this folder to the classpath used by the Scala Interpreter? Maybe scala.bat should be edited or parameterized with s...

Is there any Wicket-like web framework to use with Scala (besides lift)?

Hello I want to start a project using the Scala language. While searching for web frameworks I've found Lift. However, it is not what I was looking for: a web framework that has complete separation of HTML and code. Lift does have some nice features (and a learning curve) but we need to have complete separation of HTML and code. I was h...

Pattern matching a String as Seq[Char]

In Scala it is possible formulate patterns based on the invididual characters of a string by treating it as a Seq[Char]. An example of this feature is mentioned in A Tour of Scala This is the example code used there: object RegExpTest1 extends Application { def containsScala(x: String): Boolean = { val z: Seq[Char] = x z match...

Why does the empty string not match as Seq.empty?

During some experimentation around question Pattern matching a String as Seq[Char], I ran across another weird matching phenomenon. Consider the following code that treats a string as a sequence of characters: def %%&#(input: String) : String = { val uha : Seq[Char] = input uha match { case Seq() => "Empty" case...