scala

Need opinion regarding design/architecture of a web application

I am working on a web application which needs to get data from some local and some non local resources and then display it. As it could take arbitrary amount of time to get the data from these resources I am thinking of using the actors concept so that each actor is responsible for getting data from the respective resource. The request t...

How can I approximate Python's or operator for set comparison in Scala?

After hearing the latest Stack Overflow podcast, Peter Norvig's compact Python spell-checker intrigued me, so I decided to implement it in Scala if I could express it well in the functional Scala idiom, and also to see how many lines of code it would take. Here's the whole problem. (Let's not compare lines of code yet.) (Two notes: You...

Grammars, Scala Parsing Combinators and Orderless Sets

I'm writing an application that will take in various "command" strings. I've been looking at the Scala combinator library to tokenize the commands. I find in a lot of cases I want to say: "These tokens are an orderless set, and so they can appear in any order, and some might not appear". With my current knowledge of grammars I would hav...

How can I convert scala.xml.Elem to something compatible with the javax.xml APIs?

I have a Scala representation of some XML (i.e. a scala.xml.Elem), and I'd like to use it with some of the standard Java XML APIs (specifically SchemaFactory). It looks like converting my Elem to a javax.xml.transform.Source is what I need to do, but I'm not sure. I can see various ways to effectively write out my Elem and read it into...

Scala Newb Question - about scoping and variables

Hi, I'm parsing XML, and keep finding myself writing code like: val xml = <outertag> <dog>val1</dog> <cat>val2</cat> </outertag> var cat = "" var dog = "" for (inner <- xml \ "_") { inner match { case <dog>{ dg @ _* }</dog> => dog = dg(0).toString() case <cat>{ ct @ _* }</cat> => cat = ct(0).toString() } } /* do somethin...

Scala AST in Scala

Is there a Scala library that parses Scala and creates an Abstract Syntax Tree (AST)? Ideally I am interested in a Scala library. Plan B would be a Java library. (I know I could leverage the EBNF from the Scala Syntax Summary.) ...

What is the difference between a var and val definition in Scala?

What is the difference between a var and val definition in Scala and why does the language need both? Why would you choose a val over a var and vice versa? ...

Idiom for Scala's Option when doing equality tests

What's an easier/cleaner way to do this? val o = Some(4) if(o.isDefined) {o.get == 4} else { false } I've tried o.getOrElse(null) == 4 but that feels wrong, since in the isEmpty case, you end up testing null against the other side... which could itself be null. I need it to be if opt is defined && opt.get == whatever. I feel like s...

When should I use Scala's Array instead of one of the other collections?

This is more a question of style and preference but here goes: when should I use scala.Array? I use List all the time and occasionally run into Seq, Map and the like, but I've never used nor seen Array in the wild. Is it just there for Java compatibility? Am I missing a common use-case? ...

Using Java Lib with Scala Reserved Words

Hi, I'm using an external library written in java (selenium). One of the function calls has signature type(String,String) , and I keep getting compiler errors when trying to call it from scala, i.e.: selenium.type("ab","abc") Is there any workarounds for this issue? Thanks, ...

Can someone explain this article about enforcing race safety in Scala.

http://www.infoq.com/news/2009/07/scala-actors-race-safe-system ...

garbage collecting scala actors

Scenario: I have this code: class MyActor extends Actor { def act() { react { case Message() => println("hi") } } } def meth() { val a = new MyActor a.start a ! Message() } is the MyActor instance garbage collected? if not, how do i make sure it is? if I create an ad-hoc actor (with the 'actor' method), ...

Is there a scala identity function?

If I have something like a List[Option[A]] and I want to convert this into a List[A], the standard way is to use flatMap: scala> val l = List(Some("Hello"), None, Some("World")) l: List[Option[java.lang.String]] = List(Some(Hello), None, Some(World)) scala> l.flatMap( o => o) res0: List[java.lang.String] = List(Hello, World) Now o =>...

Type inference: Using generic methods with implicit type conversion

The problem is that you want to flatMap a List[Option[T]] to a List[T] : val l = List(Some("Hello"), None, Some("World")) to get: List(Hello, World) but there is no nice solution: l flatMap( o => o) l flatMap identity[Option[String]] l flatMap ( x => Option.option2Iterable(identity(x))) for(x <- l; y <- x) yield y The obvious so...

Scala Parser Token Delimiter Problem

I'm trying to define a grammar for the commands below. object ParserWorkshop { def main(args: Array[String]) = { ChoiceParser("todo link todo to database") ChoiceParser("todo link todo to database deadline: next tuesday context: app.model") } } The second command should be tokenized as: action = todo message =...

Why doesn't this match?

Hello. I tried to implement 'concat' as same as Haskell's 'concat' in Scala. But I failed to do. $ scala Welcome to Scala version 2.7.7.final (Java HotSpot(TM) Client VM, Java 1.6.0_14). Type in expressions to have them evaluated. Type :help for more information. scala> scala> def concat(ll:List[List[Any]]):List[Any] = { ll match { c...

parsing recursive structures in scala

I'm trying to contruct a parser in scala which can parse simple SQL-like strings. I've got the basics working and can parse something like: select id from users where name = "peter" and age = 30 order by lastname But now I wondered how to parse nested and classes, i.e. select name from users where name = "peter" and (age = 29 or age ...

Why is flatten declared on GenericTraversableTemplate and not TraversableLike?

The signature of TraversableLike.flatMap is as follows: def flatMap[B, Th](f : (A) => Traversable[B])(implicit bf : CanBuildFrom[Repr, B, Th]) : Th The signature of GenericTraversableTemplate.flatten is: def flatten[B](implicit asTraversable : (A) => Traversable[B]) : CC[B] Why is the latter method (which seems to me to differ from...

How to write "asInstanceOfOption" in Scala

Is it possible to write an "asInstanceOfOption" method that would do what is intended by the following (bogus) code? def asInstanceOfOption[T](o: Any): Option[T] = if (o.isInstanceOf[T]) Some(o.asInstanceOf[T]) else None ...

pattern matching on a series of values in scala

I'm a Scala beginner and this piece of code makes me struggle. Is there a way to do pattern matching to make sure everything i pass to Data is of the correct type? As you can see i have quite strange datatypes... class Data ( val recipient: String, val templateText: String, val templateHtml: String, val blockMaps: Map[String,List[Ma...