scala-2.8

scala 2.8 collections inconsistency?

why the methods transform (in-place mutation version of map) and retain (in-place mutation version of filter) are defined on only mutable.Map but not on mutable.Buffer and mutable.Set? shouldnt all mutable collections support these methods? ...

Scala Type inference for anonymous function declaration

Hi all, I'm a beginner in Scala and I'm just curious about how Scala handles the type inference for this code snippet trait Expression { .... } def eval (binding : String => Boolean) : Expression => Boolean I understand that binding is a function that converts String to Boolean, but why binding at the same time could be declared as ...

Could not find implicit value for parameter ordering

Hi, I get the following error when trying to compile this: Btree.scala:9: error: could not find implicit value for parameter ordering: Ordering[K] abstract class Node[K,V] extends TreeMap[K,V] TreeMap is supposed to accept an implicit Ordering[A] val which I provide. Perhaps the implicit parameter needs to be in object Tester ...

Creating an O(1)-memory Iterable from an initial object and a function which generates the next object, in Scala

I want a convenient way to generate an Iterable, given a initial object and a function to produce the next object from the current one, that consumes O(1) memory (i.e., it doesn't cache old results; if you want to iterate a second time, the function has to be applied again). It doesn't seem like there's library support for this. In Scal...

nested Annotation List in Scala

Help, how do i do stuff like the following in Scala? import org.hibernate.validator.constraints.ScriptAssert @ScriptAssert.List({ @ScriptAssert(script = "...", lang = "javascript"), @ScriptAssert(script = "...", lang = "javascript")}) ...

Scala for-comprehension returning an ordered map

How can I use a for-comprehension that returns something I can assign to an ordered Map? This is a simplification of the code I have: class Bar class Foo(val name: String, val bar: Bar) val myList: java.util.List[Foo] = ... val result: ListMap[String, Bar] = for { foo <- myList } yield (foo.name, foo.bar) I need to mak...

Scala Map implementation keeping entries in insertion order?

In Java, I use LinkedHashMap for this purpose. The documentation of Java's LinkedHashMap is very clear that it has "predictable iteration order" and I need the same in Scala. Scala has ListMap and LinkedHashMap, but the documentation on what they do exactly is poor. Question: is Scala's LinkedHashMap or ListMap the implementation to us...

How to write a zipWith method that returns the same type of collection as those passed to it?

I have reached this far: implicit def collectionExtras[A](xs: Iterable[A]) = new { def zipWith[B, C, That](ys: Iterable[B])(f: (A, B) => C)(implicit cbf: CanBuildFrom[Iterable[A], C, That]) = { val builder = cbf(xs.repr) val (i, j) = (xs.iterator, ys.iterator) while(i.hasNext && j.hasNext) { builder += f(i.next, j.ne...

How do I use the trait scala.Proxy

I just found it in the API (http://www.scala-lang.org/api/current/scala/Proxy.html) and would like to see one or two examples along with an explanation what it is good for. ...

How to correctly type-annotate this HList?

sealed abstract trait HList case class :+:[H, T <: HList](head: H, tail: T) extends HList { def :+:[T](v: T) = new :+:(v, this) } case object HNil extends HList { def :+:[T](v: T) = new :+:(v, this) } object HListExpt { def main(args: Array[String]) { val me: String :+: Int :+: Symbol :+: HNil.type = "Rahul" :+: 20 :+: 'Male...

error: value sorted is not a member of List[Int]

Welcome to Scala version 2.8.0.Beta1-prerelease (Java HotSpot(TM) Client VM, Java 1.6.0_21). Type in expressions to have them evaluated. Type :help for more information. scala> List(3, 1, 2).sorted <console>:5: error: value sorted is not a member of List[Int] List(3, 1, 2).sorted ^ Why am I getting this wei...

Passing Java array to Scala

Hello, Although I've been using Scala for a while, and mixing it with Java also, I bumped on problem. How can I pass a Java array to Scala? I know that the other way around if fairly straightforward. Java to Scala is not so however. I should I declare my method in Scala? Here is a small example of what I'm trying to achieve: Scala:...

Scala generic array

Hi, I'm trying to declare a method in an abstract class which receives an Array of generic type T. As such: abstract class Circle[-T] extends Shape[T] { def draw(points: Array[T]): Unit } The problem I'm getting is that Scala compiler complaints with: contravariant type T occurs in invariant position in type Array[T] of va...

How to get a reference to a Lift MetaMapper object by name?

In modeling an audit table, I've included fields necessary to find the original record being audited (oid: String, className: String). I'd like to programmatically find the MetaMapper for the Mapper class name. For example, if I have: class Foo extends Mapper[Foo] { def getSingleton = Foo } object Foo extends Foo with MetaMapper[Fo...

convert unparameterized Java 1.4 Collection to parameterized Scala Sequence

How can I convert a java 1.4 Collection to a Scala Seq? I am trying to pass a java-collection to a scala method: import scala.collection.JavaConversions._ // list is a 1.4 java.util.ArrayList // repository.getDir is Java legacy code val list = repository.getDir(...) perform(list) def perform(entries: List[SVNDirEntry]) = ... I alwa...

How do I idiomatically handle null checks from within Scala/Lift ?

Even with the prevalence of the Box and Option monads, we still have to check for null values here and there. The best I've come up with so far is by using the Box#!! method: (Box !! possiblyNull).map(_.toString).openOr("") Is there a better way to do this? I tried using Box's apply method: Box(possiblyNull).map(_.toString).openOr("...

Scala Set implementation to use within business model?

Let's say we want to build a big social network (because social networks are all the rage at the moment). We'll start with a simple premise that anyone who wants to use our social network should be able to register under their name and then become friends or fall out with other people registred with us: import scala.collection._ class ...

Is there an analog in Scala for the Rails "returning" method?

In Rails, one could use: returning Person.create do |p| p.first_name = "Collin" p.last_name = "VanDyck" end Avoiding having to do this: person = Person.create person.first_name = "Collin" person.last_name = "VanDyck" person I think the former way is cleaner and less repetitive. I find myself creating this method in my Scala pr...

Is this surprising (to me) difference in Map's behaviour in Scala 2.8.0 and 2.7.7 expected?

It looks like -- in Scala 2.8.0 -- if you map() a Map instance to a sequence of 2-tuples that you end up getting a Map back. When this happens, any of the 2-tuples with the same first element are considered duplicates, and you only end up getting the last one. This is different from what happened in 2.7.7. This is easier to understand...

Why is using replicate much slower than serial execution?

Hi, I've got a bit of a problem. I wanted to use scala.concurrent.ops.replicate to parallelize my program. But I found out, that the algorithm actually becomes much slower. So I wrote a little test and still got the same result. So here they are. Serial Code: Takes about 63 seconds to finish object SerTest { def main(args: Array[Stri...