scala

why can't I call methods on a for-yield expression?

Say I have some scala code like this: // outputs 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 println( squares ) def squares = { val s = for ( count <- 1 to 10 ) yield { count * count } s.mkString(", "); } Why do I have to use the temporary val s? I tried this: def squares = for ( count <- 1 to 10 ) yield { count * count }.mkStrin...

Writing functions of tuples conveniently in Scala

Quite a few functions on Map take a function on a key-value tuple as the argument. E.g. def foreach(f: ((A, B)) ⇒ Unit): Unit. So I looked for a short way to write an argument to foreach: > val map = Map(1 -> 2, 3 -> 4) map: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2, 3 -> 4) > map.foreach((k, v) => println(k)) error: wrong...

What is a good architecture for a Lift-JPA application?

I was wondering what is the best practice for a JPA model in Lift? I noticed that in the jpa demo application, there is just a Model object that is like a super object that does everything. I don't think this can be the most scalable approach, no? Is it is wise to still do the DAO pattern in Lift? For example, there's some code that loo...

Can i have a negative value as constant expression in Scala?

I have an Java-Annotation that return a double value: @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface DoubleValue { double value(); } When i try to attach the annotation to a field in a scala class and the value is negativ like here: class Test { @DoubleValue(-0.05) var a = _ } i get an compi...

scala sbt with pk11 or steps

I am quite frustrated with sbt and pk11/steps (Why are these things never work out of the box for me?) I am just trying to run "jetty-run", but i got so many dependency errors, it's not fun anymore. I am stuck with unresolved dependencies for sjson 0.3 Does anyone know which mvn repo can I get sjson 0.3 from? ...

Recursive stream throws StackOverflowError

I am defining a stream in terms of itself (a recursive definition). When trying to access the second element of the stream, StackOverflowError is thrown. The code from scala console: scala> val s1 = Stream.iterate(1)(identity _) s1: scala.collection.immutable.Stream[Int] = Stream(1, ?) scala> lazy val s2 : Stream[Int]= Stream.cons(1, (...

How do you write an idiomatic Scala Quicksort function?

I recently answered a question with an attempt at writing a quicksort function in scala, I'd seen something like the code below written somewhere. def qsort(l: List[Int]): List[Int] = { l match { case Nil => Nil case pivot::tail => qsort(tail.filter(_ < pivot)) ::: pivot :: qsort(tail.filter(_ >= pivot)) } } My an...

Setting Scala "Platform" for NetBeans 6.8 on Ubuntu Lucid Lynx

I'm trying to use NetBeans 6.8 with Scala, and it can't find the "Scala Platform" (whatever that is supposed to be). I'm using Ubuntu Lucid Lynx (fully updated). The libraries are in /usr/share/java, the binaries in /usr/bin, the docs in /usr/share/doc/scala-doc/, and the sources are uninstalled. I think that NetBeans is looking for Sca...

What is "Call By Name"?

Hi to everyone! I'm working in a homework, and the professor asked me to implement the evaluation strategy called "call by name" in scheme in a certain language that we developed and he gave us an example at http://www.scala-lang.org/node/138 in the scala language, but i don't understand in what consists the call by name evaluation stra...

[Scala] Applying overloaded, typed methods on a collection

I'm quite new to Scala and struggling with the following: I have database objects (type of BaseDoc) and value objects (type of BaseVO). Now there are multiple convert methods (all called 'convert') that take an instance of an object and convert it to the other type accordingly - like this: def convert(doc: ClickDoc): ClickVO = ... def ...

Using Scala array from java

Hi I'm trying to use some library code written in scala from a java program. I have a function that returns an Array (a scala Array) and I thought it would be possible to do Tree[] = ScalaObject.myScalaFunction() But the I get this error : [error] found : scala.runtime.BoxedArray [error] required: org.grammaticalframework.Trees.A...

Lift XML Parsing Error

I know there are other questions on this and I have read through almost all of them and none of them solved my problem. I have inside a home directory: def search(in: NodeSeq) : NodeSeq = { bind("work", in, "docId" -> text("", did = _), "visitId" -> text("", vid = _), "provider" -> text("", prov = _), "...

Migrating from Maven to SBT

As you know, SBT is compatible with Maven in some way -- SBT recognizes simple Maven POMs and can use dependencies and repositories specified in them. However, SBT wiki says that, if inline dependency is specified in SBT project definition, POM will be ignored (so using both in this case is impossible): Maven and Ivy configurations (...

how to sort a scala.collection.Map[java.lang.String, Int] by its values?

How would you sort a scala.collection.Map[java.lang.String, Int] by its values (so on the Int)? What is a short and elegant way to do that? ...

How do I return a String from a for comprehension in Scala?

Scala Newbie alert: basically I'm trying to do something like this: where I pattern match and return a String. scala> def processList(list: List[String], m: String): String={list foreach (x=> m match{ | case "test" => "we got test" | case "test1"=> "we got test1"})} ...

Help Connecting lift to an Oracle Database

So I have something like this in my boot.scala: object DBVendor extends ConnectionManager { def newConnection(name: ConnectionIdentifier): Box[Connection] = { try { Class.forName("oracle.jdbc.driver.OracleDriver") val dm = DriverManager.getConnection("jdbc:oracle:thin:@hostname:1521:orcl", "username", "password"); Fu...

Reading Lines from Sample Code in 'Programming in Scala'

Working through a sample in Chapter 3 of "Programming in Scala", the following code doesn't seem working on Scala 2.8: import scala.io.Source if (args.length > 0) { for (line <- Source.fromFile(args(0)).getLines) print(line.length + " " + line) } else Console.err.println("Filename required.") Scala complains fromFile ...

How do I create a Map type that allows multiple types for both keys and values?

I would like to create a Map type so something like the below is possible: VariantMap(1) = "Test" VariantMap("a") = 42 and VariantMap("a") would have a type of Option[Int]. Here is the code I have so far which results in Option[Nothing]: object VariantMap { import scala.reflect.Manifest private var _map= Map.empty[Any,(Manifest...

How to compose a Matcher[Iterable[A]] from a Matcher[A] with specs testing framework

If I have a Matcher[A] how do create a Matcher[Iterable[A]] that is satisfied only if each element of the Iterable satisfies the original Matcher. class ExampleSpec extends Specification { def allSatisfy[A](m: => Matcher[A]): Matcher[Iterable[A]] = error("TODO") def notAllSatisfy[A](m: => Matcher[A]): Matcher[Iterable[A]] = allSati...

scala: Adding attributes (odd and even rows) to xml table

In a Lift application, I’d like to add a special tag which takes the <tbody> part of the next table and adds odd and even classes (for example) to each <tr> tag. Alternating, of course. While I have found a way to add another attribute to all <tr> tags, there are still a few problems left (see code below). First, it doesn’t work. cycle....