scala

why have the modified if position in scala?

So I'm learning Scala and came across some examples like this: val doubleEven = for (i <- 1 to 10; if i % 2 == 0) yield i * 2 Now, what's the added benefit to having this special syntax built into the for loop as opposed to the time-honored val doubleEven = for(i <- 1 to 10){ if(i % 2 == 0) yield i*2 } style if? EDIT: Of ...

Scala problem with jMock expectations and returning a value from mock

Solved. IntelliJ didn't highlight the fact that my imports were incomplete. Hi, I have a simple Scala program that I'm trying to develop using jMock. Setting basic expectations works nicely but for some reason Scala does not understand my attempt to return a value from a mock object. My maven build spews out the following error TestLo...

Scala - how to define a structural type that refers to itself?

I'm trying to write a generic interpolate method that works on any type that has two methods, a * and a +, like this: trait Container { type V = { def *(t: Double): V def +(v: V): V } def interpolate(t: Double, a: V, b: V): V = a * (1.0 - t) + b * t } This doesn't work though (on Scala 2.8.0.RC7), I get the following er...

What are Scala's future platform concerns people should be prepared for?

At the moment Scala runs only on the JVM, with an outdated implementation for the CLR. But there are some voices at the moment, that Microsoft is interested funding an up-to-date Scala port for .NET. Considering the lack of any plan or oversight at Oracle's side what to do with Java/the JVM/the ecosystem, how can a Scala developer be p...

Why can't the first parameter list of a class be implicit?

scala> class A(implicit a: Int); defined class A scala> class B()(implicit a: Int); defined class B scala> new A()(1) res1: A = A@159d450 scala> new B()(1) res2: B = B@171f735 scala> new A(1) <console>:7: error: too many arguments for constructor A: ()(implicit a: Int)A new A(1) Why does Scalac insert an empty parameter list...

scala serialization

When I try doing the serialization examples from Ruminations of a Programmer's "JSON Serialization for Scala Objects" article I run into the error: Exception in thread "main" java.lang.NoClassDefFoundError: scala/util/parsing/syntax/Tokens. I have found that it happens when trying to deserialize. I was just wondering if there was a sim...

Strange behaviour of the Array type

scala> List(1,2,3) == List(1,2,3) res2: Boolean = true scala> Map(1 -> "Olle") == Map(1 -> "Olle") res3: Boolean = true But when trying to do the same with Array, it does not work the same. Why? scala> Array('a','b') == Array('a','b') res4: Boolean = false I have used 2.8.0.RC7 and 2.8.0.Beta1-prerelease. ...

What is a Manifest in Scala and when do you need it?

Since Scala 2.7.2 there is something called Manifest which is a workaround for Java's type erasure. But how does Manifest work exactly and why / when do you need to use it? This blog post by Jorge Ortiz explains some of it, but it doesn't explain how to use it together with context bounds. Also, what is ClassManifest, what's the diffe...

Scala: What is the idiomatic syntax for transforming nested collections?

For example: I have a list of Maps and I'd like to create a List from the values of the 3rd "column" of the maps... val l = List(Map(1 -> "test", 2 -> "test", 3 -> "test"), Map(4 -> "test", 5 -> "test", 6 -> "test")) ...

scala foreach und map initializers

Just seen an interesting possibility to initialize code blocks in Scala for high order functions such as foreach or map: (1 to 3) map { val t = 5 i => i * 5 } (1 to 3) foreach { val line = Console.readLine i => println(line) } Is this some documented feature or should i better avoid such constructs? I could imagine, ...

matching (and binding) two exception classes in one case statement in scala 2.7?

I have the following code: try { < ... some JSON parsing code .. > } catch { case e:ClassCastException => throw new ParseException(body, e) case e:JSONException => throw new ParseException(body, e) } This seems overly repetitious. I tried: case e:ClassCastException | e:JSONException => thr...

In Scala, how do I fold a List and return the intermediate results?

I've got a List of days in the month: val days = List(31, 28, 31, ...) I need to return a List with the cumulative sum of days: val cumDays = List(31, 59, 90) I've thought of using the fold operator: (0 /: days)(_ + _) but this will only return the final result (365), whereas I need the list of intermediate results. Anyway I ca...

In Scala, can I call Source.reset() on resources read from the classpath?

Suppose I have a jarfile on my classpath. In that jarfile I have a file afile.txt. I need to iterate on that file twice, once to count the lines and once to parse it. This is what I did: val source = Source.fromInputStream(/*some magic to get the resource's InputStream*/) source.getLines.foreach (/*count the lines*/) source.getLines.re...

Can I "pimp my library" with an analogue of TraversableLike.map that has nicely variant types?

Suppose I want to add functionality like map to a Scala List, something along the lines of list mapmap f, which applies the function f to each element of list twice. (A more serious example might be implementing a parallel or distributed map, but I don't want to get distracted by details in that direction.) My first approach would be o...

What happens when a Scala "Future" is garbage collected?

Say I have a Stream that's rather expensive to compute. I can easily create a thread that "computes ahead" just by writing something like import scala.actors.Futures._ val s = future { stream.size } If I then throw away the reference to this Future, will that thread be killed off by the garbage collector? ...

grouping items in an iterable by looking for a sentinel value (in scala)

I have an iterator of lines from a very large file that need to be put in groups as I move along. I know where each group ends because there is a sentinel value on the last line of each group. So basically I want to write a function that takes an iterator and a sentinel value, and returns an iterator of groups each terminated by the sent...

For my next project, a web-app, should use scala+wicket or scala+lift?

Given the various advantages of the Scala language I have decided to write my next web-application in Scala. However, should I be using Wicket or Lift? I am familiar with Wicket, a like it quite a bit, but know very little about Lift. Is learning Lift worth the effort in this context? In order words, how does Lift compare to Wicket? Giv...

What does Predef.locally do, and how is it different from Predef.identity

Looking through the Scala 2.8 Predef class, I find that there is a method "locally". As near as I can tell, it's the same as Predef.identity, except for having the "@inline" annotation. What's it for, and why is it important enough to be in Predef (and thus usable anywhere in Scala)? ...

How can I obtain Function objects from methods in Scala?

Suppose I have a simple class in Scala: class Simple { def doit(a: String): Int = 42 } How can I store in a val the Function2[Simple, String, Int] that takes two arguments (the target Simple object, the String argument), and can call doit() get me the result back? ...

Scala's type system has me

How would you implement class that parses some input via regex and transforms founded string to some other type? My approach is: class ARegex[T](regex:Regex, reform:Option[String => T]){ def findFirst(input:String):Option[T] = { (regex.findFirstIn(input), reform) match{ case (None, _) => None case (Some(s), None) => So...