scala

How do I get color coded console output from SBT on Windows?

I'm using SBT (Simple Build Tool) to build my Scala projects on Windows. I've seen that one of my friends, that runs OSX, gets color coded output in his terminal windows when running SBT, but mine is just the same color everywhere. Is there any way to enable this for Windows? ...

Adaptive Maps in Scala (or Java) Preserving Insertion Order

I would like to find and reuse (if possible) a map implementation which has the following attributes: While the number of entries is small, say < 32, underlying storage should be done in an array like this [key0, val0, key1, val1, ...] This storage scheme avoids many small Entry objects and provides for extremely fast look ups (even th...

How to reference a val in a case statement?

I'm having a slow morning. I thought referencing an existing val in a case statement would be OK. But it seems it is interpreted as a local variable definition. A rudimentary googling didn't help and I don't have my staircase book with me. In the following, what is the syntax that would allow me to match on case (b,c)? scala> val (a,b,...

Nested trait in class constructor in scala

I'm playing around with scala (scala 2.8). Suppose I have a class with a nested trait, and want to use that nested trait as the type for a parameter in the class's constructor. Is that even possible? This is the closest I've come: class OuterClass(traitParam:OuterClass#InnerTrait) { trait InnerTrait { } val y:InnerTrait = traitPa...

In Scala, how to find an elemein in CSV by a pair of key values?

For example, from a following file: Name,Surname,E-mail John,Smith,[email protected] Nancy,Smith,[email protected] Jane,Doe,[email protected] John,Doe,[email protected] how do I get e-mail address of John Doe? I use the following code now, but can specify only one key field now: val src = Source.fromFile(file) val iter =...

In Scala 2.8, how to access a substring by its length and starting index?

I've got date and time in separate fields, in yyyyMMdd and HHmmss formats respectively. To parse them I think to construct a yyyy-MM-ddTHH:mm:ss string and feed this to joda-time constructor. So I am looking to get 1-st 4 digits, then 2 digits starting from the index 5, etc. How to achieve this? List.fromString(String) (which I found her...

Should I use List[A] or Seq[A] or something else?

I was writing a class that contained some functional-esque methods. First I wrote them using List as parameters and return types. Then I thought "Hey, you could also use a more generic type!" so I replaced the Lists with Seq, hoping that I could make my stuff faster one day by feeding them something else than lists. So which general pur...

case class and traits

I want create a special calculator. I think that case class is a good idea for operations: sealed class Expr case class add(op1:Int, op2:Int) extends Expr case class sub(op1:Int, op2:Int) extends Expr case class mul(op1:Int, op2:Int) extends Expr case class div(op1:Int, op2:Int) extends Expr case class sqrt(op:Int) extends Expr case cla...

Nested iteration in Scala.

What is the difference (if any) between two code fragments below? Example from Ch7 of Programming i Scala def grep(pattern: String) = for ( file <- filesHere if file.getName.endsWith(".scala"); line <- fileLines(file) if line.trim.matches(pattern) ) println(file + ": " + line.trim) and this one def grep2(pattern...

High-performance Concurrent MultiMap Java/Scala

Dear StackOverflow, I am looking for a high-performance, concurrent, MultiMap. I have searched everywhere but I simply cannot find a solution that uses the same approach as ConcurrentHashMap (Only locking a segment of the hash array). The multimap will be both read, added to and removed from often. The multimap key will be a String an...

Scala: invoking superclass constructor.

I am experiencing a weird behavior by Scala handling of superclass constructors. I have a really simple class defined in the following way package server class Content(identifier:String,content:String){ def getIdentifier() : String = {identifier} def getContent() : String = {content} } and a simple subclass package...

How to use switch/case (smiple pattern matching) in Scala?

I've found myself stuck on a very trivial thing :-] I've got an enum: object Eny extends Enumeration { type Eny = Value val FOO, BAR, WOOZLE, DOOZLE = Value } In a code I have to convert it conditionally to a number (varianr-number correspondence differs on context). I write: val en = BAR val num = en match { case FOO => ...

SQLite for Scala

Try googling for "scala" and "sqlite". You don't come up with much. How are people interfacing with SQLite using Scala? ...

How to substract a day/hour/minute from joda-time DateTime in Scala?

I am trying to use joda-time with its Scala wrapper. Saying val dt is a DateTime and contains a date (zero time), how do I get the date of the day befor it? dt - 1.days doesn't work and gives "type mismatch" ("found: org.scala_tools.time.Imports.DateTime, required: ?{val -:?}"). Scala-time examples like 2.hours + 45.minutes + 10.sec...

How to compile standalone scala

Just starting to learn scala.. I can't seem to figure out how to compile something into a standalone application. I think I almost have a working .jar file, but keep getting a Main-Class error even though it's in the manifest, ...

In Scala 2.8, what type to use to store an in-memory mutable data table?

Each time a function is called, if it's result for a given set of argument values is not yet memoized I'd like to put the result into an in-memory table. One column is meant to store a result, others to store arguments values. How do I best implement this? Arguments are of diverse types, including some enums. In C# I'd generally use Da...

string transformation with regex

I have the following string: val s1:String = "1. my line 1\n2. my other line\n3. my 3rd line\n4. and so on" Now, I want transform at other: val s2:String = "<b>1. </b>my line 1\n<b>2. </b>my other line\n<b>3. </b>my 3rd line\n<b>4. </b>and so on" What is better way to do it? ...

Problem embedding javascript for loops in liftweb static content

Here's what I tried... I put this in a file called <mySbtBasedProjdir>/src/main/webapp/static/simpleForLoop.html <lift:surround with="default" at="content"> Why is this a problem in liftweb? <script type="text/javascript"> var i=0; for (i=0;i<=5;i++) { document.write("The number is " + i); document.write("<br />"); } <...

Funny observation about (recursive) structural types in Scala

I needed some recursive structural type in some piece of code using with traits and the structural type as type parameter constraint. It worked fine, but later I learned Scala does not support recursive structural types. So can someone explain me why this works fine: scala> trait Test[M[A] <: { def map[B](f: A => B) : M[B] } ] {} defin...

scala return on first Some in list

I have a list l:List[T1] and currently im doing the following: myfun : T1 -> Option[T2] val x: Option[T2] = l.map{ myfun(l) }.flatten.find(_=>true) The myfun function returns None or Some, flatten throws away all the None's and find returns the first element of the list if any. This seems a bit hacky to me. Im thinking that there mig...