scala-2.8

Implementing yield (yield return) using Scala continuations

How might one implement C# yield return using Scala continuations? I'd like to be able to write Scala Iterators in the same style. A stab is in the comments on this Scala news post, but it doesn't work (tried using the Scala 2.8.0 beta). Answers in a related question suggest this is possible, but although I've been playing with delimited...

Scala collection type for filter

Assume you have a List(1,"1") it is typed List[Any], which is of course correct and expected. Now if I map the list like this scala> List(1, "1") map { | case x: Int => x | case y: String => y.toInt | } the resulting type is List[Int] which is expected as well. My question is if there is an equivalent to map for fil...

Reflection on a Scala case class

I'm trying to write a trait (in Scala 2.8) that can be mixed in to a case class, allowing its fields to be inspected at runtime, for a particular debugging purpose. I want to get them back in the order that they were declared in the source file, and I'd like to omit any other fields inside the case class. For example: trait CaseClassRef...

Managing flexible, typed, immutable data structures in Scala in 2.8.x

This is a follow-up now that Scala 2.8.0 beta is out to this question: http://stackoverflow.com/questions/1710813/what-is-a-proper-way-to-manage-flexible-typed-immutable-data-structures-in-scal The new technique is to copy a case class, e.g. case class Person(name:String, email:String) val bob = Person("Bob", "[email protected]") val jill = ...

Convert Scala Set into Java (java.util.Set)?

I have a Set in Scala (I can choose any implementation as I am creating the Set. The Java library I am using is expecting a java.util.Set[String]. Is the following the correct way to do this in Scala (using scala.collection.jcl.HashSet#underlying): import com.javalibrary.Animals var classes = new scala.collection.jcl.HashSet[String] c...

scala way to define functions accepting a List of different numeric types

I have the following problem: I have a function which takes a List[Double] as parameter, performs some arithmetic operations on the elements of the list and than return the result. I would like the function also to accept List[Int]. Here is an example: def f(l: List[Double]) = { var s = 0.0 for (i <- l) s += i s } ...

cannot find class manifest for element type T

Was trying to compile some code from this SO question and run into this error message cannot find class manifest for element type T. Here is another snippet that shows the behavior: scala> def f[T](a:T, b:T):Array[T] = { new Array[T](2) } <console>:4: error: cannot find class manifest for element type T def f[T](a:T, b:T):Array[T...

function working on functions of Array[T] or List[T] or Iterable[T]

I was trying to write a testing/timing function for answers provided in this SO question. Some answers work on Array[T], some on List[T], one on Iterable[T] and one on String! What I'd like to write is a function that takes the shift* functions from the question or the answers, an input list, a predicate and an expected output and run t...

Scala: Constraint on generic class type

Hi, I am very new to Scala. I want to implement a generic matrix class "class Matrix[T]". The only constraint on T should be that T should implement a "+" and a "*" mothod/function. How do I go about doing this? For example I want to be able to use both Int, Double, and my own defined types e.g. Complex I was thinking something along ...

Scala: Implementing a subtype of Numeric[T]

How does one go about implementing a subtype of Numeric[T]? I have been looking for at guide on this but haven't found any. Example of subtypes could be Rational or Complex? Thanks in advance Troels ...

What is the difference between a view and a stream?

In the Scala 2.8 collections framework, what is the difference between view and toStream? ...

Scala Actors: Different behavior on JRE 1.5 and 1.6

My simulation is using actors and Scala 2.8-Snapshot. In Java JRE 1.5 it runs well - all 40 gears (actors) are working simultaneously. Using Java JRE 1.6 only 3 gears are working simultaneously. I tested it with and without GUI: both give same result. My simulation with GUI is available on github: http://github.com/pmeiclx/scala_gear_si...

Scala 2.8Beta1 Actor

Calling the !! method from one actor to another worker actor appears to keep the channel open even after the reply was received by the caller (ie: future is ready). For example, using !! to send 11 different messages from one actor to another worker actor will result in 11 messages similar to the below being shown in the mailbox of the ...

Transforming Scala varargs into Java Object... varargs

I have a Java class that logs stuff which has a method like this: void info(Object message, Object... params); In Scala, I've created a wrapper around such call that looks like this: def info(msg: => String, params: Any*) { log.info(msg, params); } When I call: val host = "127.0.0.1" val port = "1234" info("Start on {0}:{1}", ho...

How does the NotNull trait work in 2.8 and does anyone actually use it?

trait NotNull {} I've been trying to see how this trait can guarantee that something is not null and I can't figure it out: def main(args: Array[String]) { val i = List(1, 2) foo(i) //(*) } def foo(a: Any) = println(a.hashCode) def foo(@NotNull a: Any) = println(a.hashCode) //compile error: trait NotNull is abstract def foo(a:...

Elegant way to reverse a map in Scala

Greetings, Learning Scala currently (2.7.7) and needed to reverse a Map to do some reverse value->key lookups. I was looking for a simple way to do this, but came up with only: (Map() ++ origMap.map(kvp=>(kvp._2->kvp._1))) Anybody have a more elegant approach? ...

Equality relations in Scala

I just stumbled on one of Tony Morris' blog-posts about Java and a fundamental problem with the language: that of defining a bespoke equality-relation for a collection. This is something that I think is a big deal and wondered whether there was some scala solution. The classic issue manifests itself in thinking about, say, a trade. Let...

Problems with Scala Swing library

Hello I am having problems when using the Scala Swing library in version 2.8 Beta1-prerelease. I have a situation where I want to show a table in GUI, and update it as results are returned from a SQL request. Which way could this be done in Scala, at the moment i am using the DefaultTableModel from Java library. Another thing is that I ...

Questions about Scala from a Rubyist

I have recently been looking around to learn a new language during my spare time and Scala seems to be very attractive. I have a few questions regarding it: Will not knowing Java impose a challange in learning it? Will it be a big disadvantage later on? ( i.e How often do people rely on Java-specific libraries? ) How big of a differe...

Function.tupled and placeholder syntax

I have seen this usage of Function.tupled example in another answer: Map(1 -> "one", 2 -> "two") map Function.tupled(_ -> _.length). It works: scala> Map(1 -> "one", 2 -> "two") map Function.tupled(_ -> _.length) <console>:5: warning: method tupled in object Function is deprecated: Use `f.tuple` instead Map(1 -> "one", 2 -> "t...