scala

Changing Scala version in existing SBT project.

How can I change Scala version in existing Simple Build Tool project? I would like SBT to check whether the system's Scala version is correct and if it is not the case then download it. ...

Parsing a dynamic value with Lift-JSON

Let me explain this question with an example. If I have a JSON like the following: {"person1":{"name": "Name One", "address": {"street": "Some Street","city": "Some City"}}, "person2":{"name": "Name Two", "address": {"street": "Some Other Street","city": "Some Other City"}}} [There is no restriction on the number of perso...

Scala xhtml attribute breaks because of a question mark

I had a problem with: <iframe id="iframe1" src='http://stockcharts.com/h-sc/ui?s=MT&amp;p=D&amp;yr=2&amp;mn=0&amp;dy=0&amp;id=p43321191731' width="300px" height="300px"></iframe> in Lift web framework (Scala) version. I get: Message: java.util.NoSuchElementException scala.RandomAccessSeq$$anon$13.next(RandomAccessSeq.scala:165) sca...

Constructor with non-instance variable assistant?

I have a number of classes that look like this: class Foo(val:BasicData) extends Bar(val) { val helper = new Helper(val) val derived1 = helper.getDerived1Value() val derived2 = helper.getDerived2Value() } ...except that I don't want to hold onto an instance of "helper" beyond the end of the constructor. In Java, I'd do so...

Scala create xhtml elements dynamically

Given a String list val www = List("http://bloomberg.com", "http://marketwatch.com"); I want to dynamically generate <span id="span1">http://bloomberg.com&lt;/span&gt; <span id="span2">http://marketwatch.com&lt;/span&gt; def genSpan(web: String) = <span id="span1"> + web + </span>; www.map(genSpan); // How can I pass the loop ind...

Expand a Set[Set[String]] into Cartesian Product in Scala

I have the following set of sets. I don't know ahead of time how long it will be. val sets = Set(Set("a","b","c"), Set("1","2"), Set("S","T")) I would like to expand it into a cartesian product: Set("a&1&S", "a&1&T", "a&2&S", ..., "c&2&T") How would you do that? ...

Scala parser combinators: how to parse "if(x)" if x can contain a ")"

I'm trying to get this to work: def emptyCond: Parser[Cond] = ("if" ~ "(") ~> regularStr <~ ")" ^^ { case s => Cond("",Nil,Nil) } where regularStr is defined to accept a number of things, including ")". Of course, I want this to be an acceptable input: if(foo()). But for any if(x) it is taking the ")" as part of the regularStr and so ...

Tutorial on swing in Scala?

Is there a good tutorial showing how to use scala.swing._ ? ...

ADTs in F# and Scala

What are the key differences between ADTs in F# and Scala? Is there anything that F#'s ADTs can do but Scala's ADTs cannot (and vice versa)? ...

Scala Map conversion

I'm a Scala newbie I'm afraid: I'm trying to convert a Map to a new Map based on some simple logic: val postVals = Map("test" -> "testing1", "test2" -> "testing2", "test3" -> "testing3") I want to test for value "testing1" and change the value (while creating a new Map) def modMap(postVals: Map[String, String]): Map[String, String] =...

Scala: A class declaring itself as a variable

I'm trying to make a binary tree in scala and need to make a method for it so I'm trying to make functions inside the class that deals with children and parent. I want to make the parent a Tree so that I can recursively call it in another function called getPath but I can't create a Tree inside the Tree class. this is the code: case cl...

How to flatten list of options using higher order functions?

Using Scala 2.7.7: If I have a list of Options, I can flatten them using a for-comprehension: val listOfOptions = List(None, Some("hi"), None) listOfOptions: List[Option[java.lang.String]] = List(None, Some(hi), None) scala> for (opt <- listOfOptions; string <- opt) yield string res0: List[java.lang.String] = List(hi) I don't like t...

scala 2.8.0.RC2 compiler problem on pattern matching statement?

Why does the following module not compile on Scala 2.8.RC[1,2]? object Test { import util.matching.Regex._ val pVoid = """\s*void\s*""".r val pVoidPtr = """\s*(const\s+)?void\s*\*\s*""".r val pCharPtr = """\s*(const\s+)GLchar\s*\*\s*""".r val pIntPtr = """\s*(const\s+)?GLint\s*\*\s*""".r val pUintPtr = """\s*(const\s+)?GLuint\s*\*\s*"...

type inference still need enhance,any better suggestion for this example?

for instance in Clojure: user=> (map #(* % 2) (concat [1.1 3] [5 7])) (2.2 6 10 14) but in Scala: scala> List(1.1,3) ::: List(5, 7) map (_ * 2) <console>:6: error: value * is not a member of AnyVal List(1.1,3) ::: List(5, 7) map (_ * 2) ^ Here ::: obtain a list of type List,oops then ...

What was the reason to choose the current syntax `import foo.bar.{Baz => _}` to exclude something from import?

I wonder if there is a good reason for this optical mismatch between e. g. pattern matching, which uses a simple case foo => to denote that no action should be taken. Wouldn't it be reasonable to have something like import foo.bar.{Baz => } instead of import foo.bar.{Baz => _} considering that _ is used as an "import everythin...

Multiple return points in scala closure/anonymous function

As far as I understand it, there is no way in Scala to have multiple return points in an anonymous function, i.e. someList.map((i) => { if (i%2 == 0) return i // the early return allows me to avoid the else clause doMoreStuffAndReturnSomething(i) // thing of this being a few more ifs and returns }) raises an error: return outs...

Implementing class for pattern matching

For example, val list = List(1,2,3) list match { case a :: b => case _ => } you can match head and tail of a List using :: or tokens of ParseResult using ~. What should I do to create class that can be matched like preceding classes? UPD: And have possibility to write: case class @ ... List(1,2,3,4) match { case 1 @ 2...

Canonical pattern reference in Actors programming model.

Hello! Is there a source, which I could use to learn some of the most used and popular practices regarding Actor-/Agent-oriented programming? My primary concern is about parallelism and distribution limited to the mentioned scheme - Actors, message passing. Should I begin with Erlang documentation or maybe there is any kind of book t...

Scala type inference question

I was just pottering about with Tony Morris' excellent exercise on catamorphisms, when I was pondering what was happening in the following situation... def cata[X](some: A => X, none: => X): X Let me now call this method as follows: def isDefined: Boolean = cata( _ => true, false) I was wondering whether the type inferencer determi...

Using scala.util.control.Exception

Does anybody have good examples of using scala.util.control.Exception? I am struggling to figure it out from the types. ...