scala

Getting object instance by string name in scala

I need the object (or "singleton object" or "companion object"... anything but the class) defined by a string name. In other words, if I have: package myPackage object myObject ...then is there anything like this: GetSingletonObjectByName("myPackage.myObject") match { case instance: myPackage.myObject => "instance is what I wanted"...

Method collect on Scala 2.7

I'm looking for a collect method in scala 2.7 but I can't seem to find an applicable call. Is there something equivalent to collect that I can use in scala? To be clear I'm looking to filter elements from a list and map those filtered to a new type. ...

Understanding why "pimp my library" was defined that way in Scala

If I want to add a method to a class in Scala, I need to do something like: class RichFoo(f: Foo) { def newMethod = f.bar() } object RichFoo { implicit def foo2Rich(f: Foo) = new RichFoo(f) } Then f.netMethod will result in creation of RichFoo instance and call its method. I'm trying to understand why it was not defined similarly...

varargs puzzle?

I'm sure the answer is pretty simple, but I got stuck in this: Welcome to Scala version 2.7.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_14). Type in expressions to have them evaluated. Type :help for more information. scala> def f(x:Int*)=0 f: (Int*)Int scala> val xs:Seq[Int]=1::2::3::4::Nil xs: Seq[Int] = List(1, 2, 3, 4) ...

Need some help with Scala's instance variables

Assume this Java code: public class A { public A(String g) { x += g.length(); } private int x = 0; } If I create an instance of A, like this: A a = new A("geo"); after this call, the value of x will be 3. What am I doing wrong in my Scala code? class A(val g:String) { x += g.length var x:Int = 0 } object x ext...

Scala case classes in collections

Scenario: I am parsing an IL and want to convert from a stackbased representation to a CFG for instance. My IL consists of multiple operations like PushInt(value), Pop etc. The question is now which implementation would be correct in terms of Scala. I would love to use case classes/objects or extractors so that I can write code alà op ...

Are there any methods included in Scala to convert tuples to lists?

I have a Tuple2 of List[List[String]] and I'd like to be able to convert the tuple to a list so that I can then use List.transpose(). Is there any way to do this? Also, I know it's a Pair, though I'm always a fan of generic solutions. ...

Is it possible to curry the other way around in Scala?

Let's assume this function: def autoClosing(f: {def close();})(t: =>Unit) = { t f.close() } and this snippet: val a = autoClosing(new X)(_) a { println("before close") } is it possible to curry the first part? Something like: val a = autoClosing(_) { println("before close") } so that I could send the objects on which cl...

programmatically setting the `type` of an abstract type

class MyModel(var username:String, var password:String) extends FrameworkModel object MyModelQuery extends FrameworkQuery { type T = MyModel } trait FrameworkQuery { type T //do something with that type } So I get a class and an object where the latter is mixing in a trait which is defined as an abstract type. Is there a way...

Using Scala Actors to create sth like a Pipeline

Hello I am struggling with the following problem for a week by now and need some advice. def query(title: String): List[Search] // query("Terminator") => ["Terminator I", "Terminator II", "Terminator 1984", etc...] def searchIMDB(s: Search): List[SearchResult] def searchTMDB(s: Search): List[SearchResult] def filterRedundantSearchR...

Scala bug with immutable Map in concurrent program?

I've written a Monte Carlo player for the board game Nine Men's Morris. Everything is basically immutable. The program involves lots of futures (hundreds) and a lot of modifying immutable Maps. Sometimes I get a crash with the following exception: java.lang.NullPointerException at scala.collection.mutable.HashTable$class.elemHashCod...

Return type of "|" in Scala's parser combinators

I was reading Bernie Pope's slides on "Parser combinators in Scala". He quotes the method signature type of the "alternative" combinator |: def | [U >: T](q: => Parser[U]): Parser[U] and asks, "Homework: why doesn’t | have this type instead?" def | [U](q: => Parser[U]): Parser[Either[T,U]] ...

scala "error: io error while decoding" "with utf-8"

this thing keeps coming up I checked that all my source files are utf8 encoded, and Im using '-encoding UTF8' flag with both scalac and scala command line tools any ideas? thank you ...

Is is possible to capture the type parameter of a trait using Manifests in Scala 2.7.7?

I'm writing a ServletUnitTest trait in Scala to provide a convenience API for ServletUnit. I have something like the following in mind: /** * Utility trait for HttpUnit/ServletUnit tests * * @param [T] Type parameter for the class under test */ trait ServletUnitTest[T <: HttpServlet] { /** * Resource name of the servlet, us...

ClassCastException trying to cast retrieved JPA object to concrete class

I am not certain what I am doing wrong, but I have a class that has a class within it, so when I save the Skill class the user class also gets created, so when I do the join and I want to pull everything in at one time, I get a classcastexception. This is how I am calling my query. val retrieved_obj = em.createNamedQuery("findAllSkills...

How do I form the union of scala SortedMaps?

(I'm using Scala nightlies, and see the same behaviour in 2.8.0b1 RC4. I'm a Scala newcomer.) I have two SortedMaps that I'd like to form the union of. Here's the code I'd like to use: import scala.collection._ object ViewBoundExample { class X def combine[Y](a: SortedMap[X, Y], b: SortedMap[X, Y]): SortedMap[X, Y] = { ...

How to add a method to a Scala Enumeration object?

I'm using Scala 2.8 and defining an Enumeration like this: object Stooges extends Enumeration { type Stooge = Value val Larry, Curly, Moe = Value } And I want to add a method to this Enumeration that cycles to the "next" stooge. I can define such a method outside Stooges and this works in a test program just fine: def nextStooge(...

EntityManager fails to instantiate using JPA/Hibernate when I add a OneToMany annotation

I have been struggling with not being able to have the EntityManager be instantiated when I add a OneToMany column to a class that already has a OneToMany column. Right now the EducationInfo has a OneToOne to user, since each row will be unique to a person, but I want to be able to load all the education information given a username. I...

Printing Unicode from Scala interpreter

When using the scala interpreter (i.e. running the command 'scala' on the commandline), I am not able to print unicode characters correctly. Of course a-z, A-Z, etc. are printed correctly, but for example € or ƒ is printed as a ?. print(8364.toChar) results in ? instead of €. Probably I'm doing something wrong. My terminal supports ut...

Extracting nodes only when they have specific attributes with specific values

Building on this this post, I needed a clean way to extract nodes in a for comprehension only if they had specific attribute values. Here's what I came up with: def attributeValue(attrs: (String, String)*)(n: Node) = attrs.map { p => n.attribute(p._1).exists(_ == p._2) } reduceLeft(_ && _) And here's an example that uses it t...