scala

Scala println in a for loop

The following Scala code does just what I expect it to - it prints each line of some_file.txt. import scala.io.Source val lines = Source.fromPath("some_file.txt").mkString for (line <- lines) print(line) If I use println instead of print, I expect to see some_file.txt printed out with double-spacing. Instead, the program prints ...

How would I express a chained assignment in Scala?

How would I express the following java code in scala? a = b = c; By the way, I'm re-assigning variables (not declaring). ...

graph library for scala

Is there a good library (or wrapper to Java library) for graphs, and/or graph algorithms in scala? This one seems to be quite dead. This is an example for the Dijkstra algorithm in scala, but I'm looking for a library a-la JGraphT. ...

Scala: how to specify varargs as type?

Instead of def foo(configuration: (String, String)*) I'd like to be able to write: type Configuration = (String, String)* def foo(configuration: Configuration) The main use case is to provide an easy method signature when overriding in subclasses UPDATE: I can come close by type Param = (String, String) def foo(configuration: P...

How to split and dispatch an async control-flow using Continuations?

Hello, I have an asynchronous control-flow like the following: ActorA ! DoA(dataA, callback1, callbackOnErrorA) def callback1() = { ... ActorB ! DoB(dataB, callback2, callbackOnErrorB) } def callback2() = { ActorC ! DoC(dataC, callback3, callbackOnErrorC) } ... How would I divide this flow into several parts (continuations)...

Scala: can't write setter without getter?

This works: class ButtonCountObserver { private var cnt = 0 // private field def count = cnt // reader method def count_=(newCount: Int) = cnt = newCount // writer method // ... } val b = new ButtonCountObserver b.count = 0 But this doesn't class ButtonCountObserver { private var cnt = 0 // private field def coun...

How do I specify a static array in a Scala 2.8 annotation?

I've been building out some annotated domain classes in Scala 2.8.0 using Hibernate Annotations 3.4.0. It's been working fine, except that there are certain annotations which take an array as a parameter. For example, here's a Java annotation that I want to express in Scala: @OneToMany(mappedBy="passport_id", cascade=CascadeType.PERSIST...

Scalac command line parameters in eclipse?

Scala includes the continuations plugin now (yay), but must be enabled by passing "-P:continuations:enable" to the scala compiler. Is there a way to pass arbitrary arguments to scalac for the eclipse scala plugin? From: http://permalink.gmane.org/gmane.comp.lang.scala/19439 the plugin is loaded by default, but it must be enabled ...

Cost of using repeated parameters

I consider refactoring few method signatures that currently take parameter of type List or Set of concrete classes --List[Foo]-- to use repeated parameters instead: Foo*. Update: Following reasoning is flawed, move along... This would allow me to use the same method name and overload it based on the parameter type. This was not po...

When is @uncheckedVariance needed in Scala, and why is it used in GenericTraversableTemplate?

@uncheckedVariance can be used to bridge the gap between Scala's declaration site variance annotations and Java's invariant generics. scala> import java.util.Comparator import java.util.Comparator scala> trait Foo[T] extends Comparator[T] defined trait Foo scala> trait Foo[-T] extends Comparator[T] <console>:5: error: contrav...

Scala version of Rubys' each_slice?

Does Scala have a version of Rubys' each_slice from the Array class? ...

Launch Scala REPL programatically?

I would like to launch a Scala Swing application from the command line, then after the application is started, drop into the Scala REPL to use as a control interface. Ideally I would also like to pre-bind some variable names. Even better would be using a Java2D terminal emulator for the REPL, but I couldn't find anything appropriate. D...

O(1) conversion from mutable.Map to immutable.Map?

Is there a way to convert (wrap) a mutable Map to immutable in O(1) time (that is, not by copying the values, but similar to what is done in JavaConversions) ...

Scala and the Java Memory Model

The Java Memory Model (since 1.5) treats final fields differently to non-final fields. In particular, provided the this reference doesn't escape during construction, writes to final fields in the constructor are guaranteed to be visible on other threads even if the object is made available to the other thread via a data race. (Writes to ...

Tool for converting subset of PHP code into Java/Scala/Groovy?

Do any exist? Ideally it would also come with a library of basic PHP functions. I have a bunch of simple PHP scripts (no extensions, no fancy dynamic hacks, etc) I'd like to convert to Java... It would be great if a tool could do 80% of the work while I do the other 20%. ...

Dependency between operations in scala actors

I am trying to parallelise a code using scala actors. That is my first real code with actors, but I have some experience with Java Mulithreading and MPI in C. However I am completely lost. The workflow I want to realise is a circular pipeline and can be described as the following: Each worker actor has a reference to another one, thus...

scala Slider throws casting exception

hello, I create an Slider object: val slider = new Slider{ min = 0 max = 30 labels = Map(0 -> new Label("Nula"),15-> new Label("Pul"),30-> new Label("Max")) paintLabels = true } when I run this, an exception is thrown: scala.swing.Label cannot be cast to java.awt.Component but why? When i browse...

Unit testing several implementation of the same trait/interface

I program mostly in scala and java, using scalatest in scala and junit for unit testing. I would like to apply the very same tests to several implementations of the same interface/trait. The idea is to verify that the interface contract is enforced and to check Liskov substitution principle. For instance, when testing implementations of...

Is there an easy way to integrate scaladoc into IntelliJ Idea?

How do I use/generate scaladoc from within IntelliJ Idea (running on ubuntu)? Most preferably I would like to configure Tools/generate javadoc to also generate scaladoc, though that might be hard as of the differences between both according to this thread. ...

best scala idiom for find & return

This is something I encounter frequently, but I don't know the elegant way of doing. I have a collection of Foo objects. Foo has a method bar() that may return null or a Bar object. I want to scan the collection, calling each object's bar() method and stop on the first one returning an actual reference and return that reference from the ...