scala

why FunctionN(0-22) ProductN(1-22) TupleN(1-22)?

The api has FunctionN(0-22) ProductN(1-22) TupleN(1-22) the question is: 1.why the number is end of 22? why not 21 or 23? 2.why Function is start with 0 ? but Product and Tuple are not? ...

Generate lazy "spiral" in Scala

Task: For a given position in 2D array generate list of surrounding positions located in radius. For example: input: (1, 1) radius: 1 output: ( (0, 0), (1, 0), (2, 0), (0, 1), (2, 1), (0, 2), (1, 2), (2, 2) ). I wrote something like def getPositions(x:Int, y:Int, r:Int) = { for(radius <- 1 to r) yield ...

Java multiple inheritance

I have two Java class hierarchies that share a common ancestor and implement a common interface. I need to pass a pointer to one of these things to a method in another class. interface I { ... } class A extends java.awt.Component implements I { ... } class B extends java.awt.Component implements I { ... } class D { Component c; I ...

Scala: Parsing transformation question

I am using Scala's combinator parser as follows: def a = b ~ c ^^ { case x ~ y => A(x,y) } def b = ... { B() } def c = ... { C() } now I have a feature change that change within the parsing of the reference of previously parsed B to be a val in C. So C's constructor is something like: C(ref:B) I can imagine, the only way to achieve...

Inheritance in Lift Mapper or Record Framework

Is there a way to define proper a inheritance model in Lift using Mapper o Record Framework where there is a table for the parent class and one table for each son? ...

How does one prove the equivalence of two types and that a signature is singly-inhabited?

Anyone who has been following Tony Morris' blog and scala exercises, will know that these two type signatures are equivalent: trait MyOption1[A] { //this is a catamorphism def fold[B](some : A => B, none : => B) : B } And: trait MyOption2[A] { def map[B](f : A => B) : MyOption2[B] def getOrElse[B >: A](none : => B) : B } F...

Is there an API method that compares contents of a Seq irrespective of order?

Assuming: val l1 = List(1,2,3) val l2 = List(2,3,1) I want a method that confirms that l1 is equal to l2 (as in same contents but different order). Is there an API method on List/Seq to do this? l1.sameElements(l2) does not work as it verifies order as well. I've come up with the following: l1.foldLeft(l1.size == l2.size)(_ && l...

Using Scala's Delimited Continuations for implicit Monads

Hello, I'm playing with some kind of DSL defined by an monadic interface. Since applying the monad using a bunch of flatMap applications is kind of cumbersome and I find for-comprehension syntactically not that beautiful, I'm trying to implicitely mix monadic and non monadic code using delimited continuations. It's actually working fi...

Scala actors exception "react on channel belonging to other actor"

Given the following code: class A extends Actor { def act() { loop { reactWithin(1000) { case _ => println("A Message") } } } } and class B extends A { val test = Actor.actor { loop { reactWithin(1000) { case "B" => println("B Message") } } } } Creating an instance of ...

Sbt and the Fast Scala Compiler?

Does sbt make use of fsc? For test purposes I am compiling a 500-line program on a fairly slow Ubuntu machine (Atom N270). Three successive compile times were 77s, 66s, and 66s. I then compiled the file with fsc from the command line. Now my times were 80s, 25s, 18s. Better! That implies to me sbt is not using fsc. Am I right? If...

Using Scala to cut up a large CSV file

What's the best way to do file IO in Scala 2.8? All I want to do is cut a massive CSV file into lots of smaller ones with, say 1000 lines of data per file, and each file retaining the header. ...

Is lack of 3rd party libraries preventing you using Scala?

I started learning Scala the other day. As for the language itself, I think it's fantastic, no problems there at all. To aid in my learning process I set myself a task, to download, parse and index text from HTML pages. In doing the above I found myself constantly digging into existing Java libraries. I found that I had to use Java libr...

Can java run a compiled scala code?

Can command java run a compiled scala code? If so, why do we have an exclusive command scala? ...

Text editor for scala

I'm tired of using IDEs for scala because it can take several minutes to write one line of code on my computer (before I started programming in scala I didn't surmise that it is slow). It's a great pity that there is no option to turn off some features of scala plugin (of any IDE) that devour 100% of my cpu power and not necessary for me...

IntMap changes type after innocent mapping

consider this piece of code:Welcome to Scala version 2.8.0.r0-b20100714201327 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_20). scala> val a = IntMap((1,1)) a: scala.collection.immutable.IntMap[Int] = IntMap((1,1)) scala> a.map(x => (x._1,x._2 + 1)) res23: scala.collection.immutable.Map[Int,Int] = Map((1,2)) header of IntMap.map says t...

Why aren't my scala futures more efficient ?

I'm running this scala code on a 32-bit quad-core Core2 system: def job(i:Int,s:Int):Long = { val r=(i to 500000000 by s).map(_.toLong).foldLeft(0L)(_+_) println("Job "+i+" done") r } import scala.actors.Future import scala.actors.Futures._ val JOBS=4 val jobs=(0 until JOBS).toList.map(i=>future {job(i,JOBS)}) println("Running....

How do I call a field accessor in Scala using Java reflection?

If I have a small Scala class with a private field and public accessors: class Entity { private var _name:String = "" def name:String = <some stuff> def name_=(v:String) = <some stuff> } How can I invoke these accessors using Java reflection? The class may be 3rd party code, or at least really hard to change. Ple...

Auto-configure Scala facet in IntelliJ IDEA for mixed Scala/Java Maven project

How do I get IntelliJ to auto-configure the Scala facet in a Maven project with mixed Scala and Java source code? I am using Scala Plugin Nightly for Maia Build 2099. mvn compile and mvn test both work from the command prompt and from the Maven Projects panel in IntelliJ. However, if I try to run ScalaSpec directly in IntelliJ it prese...

Strange OutOfMemoryError on Stream

Why do I get OutOfMemoryError for the following code? Stream.from(1).filter(n => (1 to 20).forall(x => n % x == 0)).head ...

Why double arrow for Scala and C# lambdas?

Does anyone know some background, why Scala and C# use double arrow (=>) for lambdas instead of single arrow (->)? Single arrow is more common in literature, is used in Haskell, O'Caml, F#, Groovy etc. and IMO it also looks nicer :) ...