scala

When is a scala partial function not a partial function?

While creating a map of String to partial functions I ran into unexpected behavior. When I create a partial function as a map element it works fine. When I allocate to a val it invokes instead. Trying to invoke the check generates an error. Is this expected? Am I doing something dumb? Comment out the check() to see the invocation. ...

Need help figuring out scala compiler errors.

Hello all, I have been working on a project in scala, but I am getting some error messages that I don't quite understand. The classes that I am working with are relatively simple. For example: abstract class Shape case class Point(x: Int, y: Int) extends Shape case class Polygon(points: Point*) extends Shape Now suppose that I create...

How do I exclude/rename some classes from import in Scala?

Language FAQ says import scala.collection.mutable.{_, Map => _, Set => _} should import all classes from package scala.collection.mutable, except Map and Set. But it gives me this error: error: '}' expected but ',' found. import scala.collection.mutable.{_, Map => _, Set => _} Is there still a way to do this? ...

Scala 2.8: type inference of anonymous functions as default parameters

In Scala 2.8.0 RC 2 this definition: def buttonGroup[T](values: Array[T], textProvider: T => String = (t: T => t.toString)) = ... gives the error message: not found: value t def buttonGroup[T](values: Array[T], textProvider: T => String = (_.toString)) = ... gives missing parameter type for expanded function ((x$1) =>...

Scala Hoogle equivalent?

Hoogle allows you to search many standard Haskell libraries by either function name, or by approximate type signature. I find it very useful. Is there anything like Hoogle for Scala? Search in ScalaDoc 2 only finds types and packages by name. ...

Scala 2.8 weird type error

Types: def radioGroup[T](values: Array[T], textProvider: T => String)(setups:(RadioGroup[T] => Unit)*)(parent: Composite): RadioGroup[T] def label(setups:(Label => Unit)*)(parent:Composite): Label def contains(setups : (Composite => Unit)*) : Composite // "Pimp my library" on Composite This works: new Composite(parent, SWT.NONE).c...

Does anyone have any recommendations for learning about Scala 2.8 changes?

Up until now I've been using Scala 2.7.7 (with Programming in Scala as my main reference). But as I'm a relative n00b, and 2.8 seems as if it will be out soon, I thought it would be a good idea to start using 2.8 before I get into any bad/outdated habits. I've seen http://www.scala-lang.org/node/1564 as a list of key new features, but d...

How roughen (as opposed to flatten) a list in a functional style?

For instance, I have a list (1 2 3 4 5 6 7 8 9 10 11), and want to roughen it by 3 elements (or another length) to get ((1 2 3) (4 5 6) (7 8 9) (10 11)). What pretty code could I use for this? Thanks. ...

Why is Scala's type inferencer not able to resolve this?

In the code snippet below - why do I have to give a type annotation for Nil? Welcome to Scala version 2.8.0.RC2 (OpenJDK Server VM, Java 1.6.0_18). Type in expressions to have them evaluated. Type :help for more information. scala> List(Some(1), Some(2), Some(3), None).foldLeft(Nil)((lst, o) => o match { case Some(i) => i::lst; case No...

Best way to score and sum in Scala?

Is there a better way of doing this: val totalScore = set.foldLeft(0)( _ + score(_) ) or this: val totalScore = set.map(score(_)).sum I think it's quite a common operation so was expecting something sleeker like: val totalScore = set.sum( score(_) ) ...

How to find index of element with minimum value?

Say I have a list val list = List(34, 11, 98, 56, 43). Now how do I find the index of the minimum element of the list (e.g. 1 in this case)? ...

Polymorphism, Autoboxing, and Implicit Conversions

Would you consider autoboxing in Java to be a form of polymorphism? Put another way, do you think autoboxing extends the polymorphic capabilities of Java? What about implicit conversions in Scala? My opinion is that they are both examples of polymorphism. Both features allow values of different data types to be handled in a uniform m...

Dividing a list in specific number of sublists

I want to divide a list in "a specific number of" sublists. That is, for example if I have a list List(34, 11, 23, 1, 9, 83, 5) and the number of sublists expected is 3 then I want List(List(34, 11), List(23, 1), List(9, 83, 5)). How do I go about doing this? I tried grouped but it doesn't seem to be doing what I want. PS: This is no...

How to implement collection with covariance when delegating to another collection for storage?

I'm trying to implement a type of SortedMap with extended semantics. I'm trying to delegate to SortedMap as the storage but can't get around the variance constraints: class IntervalMap[A, +B](implicit val ordering: Ordering[A]) //extends ... { var underlying = SortedMap.empty[A, List[B]] } Here is the error I get. I understand w...

Why does the definition of Array.map in Scala is "throw new Error()"

The source code of map for Array is: override def map[B](f: A => B): Array[B] = throw new Error() But the following works: val name : Array[String]= new Array(1) name(0)="Oscar" val x = name.map { ( s: String ) => s.toUpperCase } // returns: x: Array[java.lang.String] = Array(OSCAR) ...

How to exclude R*.class files from a proguard build

I am one step away from making the method described here: http://stackoverflow.com/questions/2761443/targeting-android-with-scala-2-8-trunk-builds work with a single project (vs one project for scala and one for android). I've come across a problem. Using this input file (arguments to) proguard: -injars bin;lib/scala-library.jar(!MET...

How can I run a package created with Simple Build Tool?

I run: $ echo 'object Hi { def main(args: Array[String]) { println("Hi!") } }' > hw.scala $ sbt > warn Set log level to warn > run Hi! > package $ java -jar target/scala_2.7.7/test_2.7.7-1.0.jar Exception in thread "main" java.lang.NoClassDefFoundError: scala/ScalaObject at java.lang.ClassLoader.defineClass1(Native Method) at java.lang...

How to get methods list in scala

In language like python and ruby to ask the language what index-related methods its string class supports (which methods’ names contain the word “index”) you can do “”.methods.sort.grep /index/i And in java List results = new ArrayList(); Method[] methods = String.class.getMethods(); for (int i = 0; i < methods.length; i++) { ...

How to add a new page in Lift framework

How can I add a new page in the webapp directory in lift that can be accessed by users? Currently only the index.html can be accessed through http://localhost:8080/ or http://localhost:8080/index.html Say I add a static file newpage.html into webapp dir, then what can I do so users can access it through http://localhost:8080/newpage.ht...

Making stand-alone jar with Simple Build Tool.

Is there a way to tell sbt to package all needed libraries (scala-library.jar) into the main package, so it is stand-alone? (static?) ...