scala

generator/block to iterator/stream conversion

Basically I want to convert this: def data(block: T => Unit) to a Stream (dataToStream is a hypothetical function that do this conversion): val dataStream: Stream[T] = dataToStream(data) I suppose this problem could be resolved by continuations: // let's assume that we don't know how data is implemented // we just know that it gen...

How to ZIP files with a prefix folder in SBT

To generate a distribution ZIP with Simple Build Tool one can simply do def distPath = ( ((outputPath ##) / defaultJarName) +++ mainDependencies.scalaJars ) lazy val dist = zipTask(distPath, "dist", "distribution.zip") dependsOn (`package`) describedAs("Zips up the project.") This adds the JAR files into the root of the ZIP. How d...

":" in type parameter

In scala-arm project, I see code like this: def managed[A : Resource : Manifest](opener : => A) : ManagedResource[A] = new DefaultManagedResource(opener) Can someone explain the meaning of [A : Resource : Manifest] ? ...

scala: parser help

I'm learning to write a simple parser-combinator. I'm writing the rules from bottom up and write unit-tests to verify as I go. However, I'm blocked at using repsep() with whitespace as the separator. object MyParser extends RegexParsers { lazy val listVal:Parser[List[String]]=elem('{')<~repsep("""\d+""".r,"""\s+""".r)~>elem('}') } T...

Scala and Annotation

Just thinking of implementing Guice in scala Any sample code ? ...

FSC recompiles every time

FSC recompiles my .scala files every time even there is no need - I can compile it twice without editing anything between attempts and it recompiles them! For example, I have 2 files Hello.scala class Hello{ print("hello") } And Tokens.scala: abstract class Token(val str: String, val start: Int, val end: Int) {override def toSt...

How do I read response headers + body from single POST using scala's dispatch lib

Hi, I have been trying to use the dispatch library to download a file via an http POST request. The server returns a "content-disposition" header suggesting a filename for the data file it returns. I have succeeded reading the entire response body as a string, http(r >~ { (x) => println(x.getLines.mkString("","\n","")) }) reading th...

Why does Eclipse JVM hang when executing external processes?

Hey all, I am writing some simple Scala code to control the lifecycle of a MySQL server (start it, create appropriate tables and users, etc...) on Windows XP. For example, I have a function which starts the mysql daemon in an external shell. The function should exit as soon as mysql has started (it must not wait for it to be finished)...

scala: How to get the class in its own constructor

I need access to the Class of the object being constructed in its own constructor (for various detailed reasons I don't think are relevant to my question). I want something like this class Foo(val i:Int) class Bar extends Foo(this.getClass.getName.length) val b = new Bar println(b.i) to print 3 ("Bar".length). But it doesn't. If th...

scaladoc for map on Map

According to the scaladoc for the map method on a Map object, it should return a new Map: def map [B] (f: ((A, B)) ⇒ B) : Map[B] "returns a new map resulting from applying the given function f to each element of this map and collecting the results. " But it doesn't: scala> val countries = Map("NO" -> "Norway", "US" -> "Unit...

Changing sbt project's directory layout

According to sbt tutorial on changing paths I'm trying to change "target" output directory to "someother" override def outputDirectoryName = "someother" Everything goes fine except one: sbt automatically creates target directory with ".history" file inside. Why sbt does this when it supposed do create only "someother" dir ? I tryied t...

Implicit parameter resolution problem for higher kinded types

Consider the following code: object foo { trait Bar[Q[_]] implicit object OptionBar extends Bar[Option] def test[T, C[_]](c: C[T])(implicit bar: Bar[C]) = () def main(args: Array[String]) { test(Some(42): Option[Int]) //??? } } This works, but I need to type the Some(42) as Option[Int], else the implicit...

Executors run longer than timeout value

Here is a code segment of scala. I set timeout as 100 mills. Out of 10000 loops, 106 of them run more than 100 mills without throwing exceptions. The largest one is even 135 mills. Any reason why this is happening? for (j <- 0 to 10000) { total += 1 val executor = Executors.newSingleThreadExecutor val result = executor.submit[Int...

in regards of finding all nodes that have an attribute that matches a certain value with scala

Hi, I saw the following example disccussed here previously, where the goal was to return all nodes that contain an attribute with an id of X that contains a value Y: //find all nodes with an attribute "class" that contains the value "test" val xml = XML.loadString( """<div> <span class="test">hello</span> <div class="test"><p>hel...

How to do simple type cast in Scala?

This should be a silly question. scala> val aFloat = 1.5f aFloat: Float = 1.5 How to cast aFloat to an Int in a simple way? I already know to use a.asInstanceOf[Int]. But it needs too much keystrokes. ...

Is there any ScalaConsole around?

when using GroovyConsole I've found that i'ts much more useful then scala command-line REPL. Does anyone know will it be ScalaConsole anytime soon? It will certainly help to fiddle with Scala and learn its ways ...

Scala CheatSheet Quickref

Do you know where to find good cheetsheet of Scala? except wellknown http://anyall.org/scalacheat/ I would prefer good DZone Refcard ...

Scala: Metaobject for traits

In Scala, there are metaobjects like java.lang.Class and java.lang.reflect.Method. But what metaobject exists for traits? class Foo { def foo = "foo" } var obj = classOf[Foo] obj // java.lang.Class[Foo] = interface Foo It prints interface. What is this? ...

Google app engine, multiple languages

Hi, i'm working on on a project with DJango but i'm also thinking about going the Jython route. By doing so...since i'll be using the java instance instead of cpython wouldn't I be able to use java, scala, ruby and other other languages that run on top of the jvm if need be? ...

Scala: Do implicit conversions work on Any?

I would like to store some objects from different type hierarchy into List[Any] or similar container, but perform implicit conversions on them later on to do something like type class. Here is an example: abstract class Price[A] { def price(a: A): Int } trait Car case class Prius(year: Int) extends Car trait Food case class FriedChic...