scala

Can I zip more than two lists together in Scala?

Given the following Scala List: val l = List(List("a1", "b1", "c1"), List("a2", "b2", "c2"), List("a3", "b3", "c3")) How can I get: List(("a1", "a2", "a3"), ("b1", "b2", "b3"), ("c1", "c2", "c3")) Since zip can only be used to combine two Lists, I think you would need to iterate/reduce the main List somehow. Not surprisingly, the f...

Unimporting in Scala

I heard recently some advice to "unimport an implicit conversion from Predef" - I presume that this means it is possible to unimport unwanted classes too: import java.awt._ unimport java.awt.List But this is not the syntax of an "unimport" (i.e. there is no such unimport keyword). What is the correct syntax? ...

How the Scala script that reads 5G log file from network drive should be modified in order to read last x lines (like 'tail' in Unix)?

How the Scala script that reads 5G log file from network drive should be modified in order to read last x lines (like 'tail' in Unix)? ::#! @echo off call scala %0 %* goto :eof ::!# import scala.io.Source if (args.length > 0) { for (line <-Source.fromFile(args(0)).getLines) if(line.contains("percent")){ print(line) } } ...

Selection sort in functional Scala

I'm making my way through "Programming in Scala" and wrote a quick implementation of the selection sort algorithm. However, since I'm still a bit green in functional programming, I'm having trouble translating to a more Scala-ish style. For the Scala programmers out there, how can I do this using Lists and vals rather than falling back i...

Does Scala support tail recursion optimization?

Does Scala support tail recursion optimization? ...

Why are my Scala types not matching?

I have the following variable series: var series: List[FlotSerie] = List( new FlotSerie() { override val label = Full("Min") }, new FlotSerie() { override val label = Full("Max") }, new FlotSerie() { override val label = Full("Avg") } ) Unfortunately, I am getting a compiler error with the following method, whi...

Is there a safe way in Scala to transpose a List of unequal-length Lists?

Given the following List: val l = List(List(1, 2, 3), List(4, 5), List(6, 7, 8)) If I try to transpose it, Scala will throw the following error: scala> List.transpose(l) java.util.NoSuchElementException: head of empty list at scala.Nil$.head(List.scala:1365) at scala.Nil$.head(List.scala:1362) at scala.List$$anonfun$trans...

What is scala -i command-line option supposed to do ?

I'm finding the scala '-i' command line option quite useful for running some scala code and then dumping me into an interactive shell so I can prod/inspect the things it defined. One thing which completely mystifies me though: why does it load and run the script twice ? For example, given file test.scala containing the cannonical prin...

Problem with Scala Dispatch Databinder library

I am using the Dispatch Databinder library for Http in Scala. I have this method. def testCheckPage(url:String):String = { try { var http = new Http var request = new Request(url) val req_with_agent = request <:< Map("User-Agent" -> "Mozilla/4.0") val responseBody = Http (req_...

Java/Scala BigInteger Pasting

I have a problem with the Java BigInteger class: I can't paste a large value into BigInteger. For example, let's say I want to assign a BigInteger to this number: 26525285981219105863630848482795 I cannot assign it directly, because the compiler thinks it's an integer: val bi = 26525285981219105863630848482795 //compile error But I...

Scala Regex Multiple Block Capturing

Hi there I'm trying to capture parts of a multi-lined string with a regex in Scala. The input is of the form: val input = """some text |begin { | content to extract | content to extract |} |some text |begin { | other content to extract ...

Scala: How to know if a class is an enumeration; isInstanceOf[Enumeration] doesn't work

I'm in scala writing a serializer that saves an object (or Model) to the database (for app engine), and I need to treat some fields as special cases. For example, if the field is of type Array[Byte], I Save it as a blob. And I need to treat Enumerations as special cases too, but I can't find out how to know if a type is an enumeration. ...

Scala and html parsing

How do you load an html dom document into scala. The XML singleton had errors when trying to load the xlns tags. import java.net._ import java.io._ import scala.xml._ object NetParse { import java.net.{URLConnection, URL} import scala.xml._ def netParse(sUrl:String): Elem = { var url = new URL(sUrl) var connect...

How to implement efficient sorting algorithms for multiple processors with Scala?

How to implement efficient sorting algorithms for multiple processors in Scala? Here's the link for radix algorithm in GPU: radix algorithm in GPU ...

Why can't scalac optimize tail recursion in certain scenarios?

Why doesn't scalac (the Scala compiler) optimize tail recursion? Code and compiler invocations that demonstrates this: > cat foo.scala class Foo { def ifak(n: Int, acc: Int):Int = { if (n == 1) acc else ifak(n-1, n*acc) } } > scalac foo.scala > jd-gui Foo.class import scala.ScalaObject; public class Foo implements S...

fold list of tuples in scala with destructuring

scala> val l = List((1,2), (2,3)) l: List[(Int, Int)] = List((1,2), (2,3)) I can do scala> (0 /: l) {(a, i) => i._1 + a} res20: Int = 3 But I want to be able to name the tuple's elements. Something like: scala> (0 /: l) {(a, (b,c)) => b + a} <console>:1: error: not a legal formal parameter (0 /: l) {(a, (b,c)) => b + a} ...

Scala and tail recursion

There are various answers on Stack Overflow which explain the conditions under which tail recursion is possible in Scala. I understand the limitations and how and where I can take advantage of tail recursion. The part I don't understand is why the limitation to private or final methods exists. I haven't researched how the Scala compiler...

Cannot get FeatureSpec structure to show up in the output when combined with JUnitSuite

I am trying to use Scala's capabilty of running a class with either JUnit or ScalaTest. I took the example given for FeatureSpec and combined it with the example using JUnitSuite. I am having a problem generating the given/when/then output of Scalatest.FeatureSpec structure in the output (console) when combined with JUnitSuite. Any and ...

Scala: XML Whitespace Removal?

Anyone know of a good scala library to do whitespace removal/compaction from XML? <foo> <bar>hello world</bar> <baz> xxx </baz> </foo> to: <foo><bar>hello world</bar><baz>xxx</baz></foo> -harryh ...

Include jar file in Scala interpreter

Is it possible to include a jar file run running the Scala interpreter? My code is working when I compile from scalac: scalac script.scala -classpath *.jar But I would like to be able to include a jar file when running the interpreter. ...