scala

Suggested content for a lunch-time "Introduction to Scala" talk

I'm going to be giving a short (30-40 mins) lunch-time talk on Scala to technical staff at my company. I'd like some suggestions for what would be the most appropriate content. Most people attending will have experience in Java and/or C# (plus various other languages). What are the key things to cover? I'd like to give a brief introdu...

Scala Actors suspends unexpected when connecting to a database

I have a problem with my understanding of the standard actor library in Scala. In the code below I have created a simple swing, which basically should test if it is able to connect to a postgreSQL server. However it doesnt make it that far, I use Actors since the UI otherwise would freeze up while doing the work needed to connect to the ...

Scala: Given a scala.xml.Node, what's the most efficient way of getting the second (or n-th) child element?

Given a scala.xml.Node object (with white space and elements as child nodes) what's the most efficient way of getting the second (or n-th) child element? Usually I would go for the built-in (node \ "foo"), but sometimes I have to rely on the position of the element. For example, I could have two Choice groups that could be either foo or...

Scala Actors: Different behavior on JRE 1.5 and 1.6

My simulation is using actors and Scala 2.8-Snapshot. In Java JRE 1.5 it runs well - all 40 gears (actors) are working simultaneously. Using Java JRE 1.6 only 3 gears are working simultaneously. I tested it with and without GUI: both give same result. My simulation with GUI is available on github: http://github.com/pmeiclx/scala_gear_si...

functional programming scala map and fold left

Hi can someone tell me some good tutorials on fold left and map ...

How to get an java.lang.Class of a scala object for an annotation argument

I have an object and I need to pass its class to an annotation that takes java.lang.Class, eg: public @interface PrepareForTest { Class<?>[] value() } object MyObject @PrepareForTest(Array(?????)) class MySpec ... I've tried: @PrepareForTest(Array(classOf[MyObject])) // error: not found: type MyObject @PrepareForTest(Array(MyOb...

Functional equivalent of if (p(f(a), f(b)) a else b

I'm guessing that there must be a better functional way of expressing the following: def foo(i: Any) : Int if (foo(a) < foo(b)) a else b So in this example f == foo and p == _ < _. There's bound to be some masterful cleverness in scalaz for this! I can see that using BooleanW I can write: p(f(a), f(b)).option(a).getOrElse(b) But ...

JVM OutOfMemory error "death spiral" (not memory leak)

We have recently been migrating a number of applications from running under RedHat linux JDK1.6.0_03 to Solaris 10u8 JDK1.6.0_16 (much higher spec machines) and we have noticed what seems to be a rather pressing problem: under certain loads our JVMs get themselves into a "Death Spiral" and eventually go out of memory. Things to note: ...

Immutable Map implementation for huge maps

If I have an immutable Map which I might expect (over a very short period of time - like a few seconds) to be adding/removing hundreds of thousands of items from, is the standard HashMap a bad idea? Let's say I want to pass 1Gb of data through the Map in <10 seconds in such a way that the maximum size of the Map at any once instant is on...

Comparison of performance between Scala etc. and C/C++/Fortran?

Hi all, I wonder if there is any reliable comparison of performance between "modern" multithreading-specialized languages like e.g. scala and "classic" "lower-level" languages like C, C++, Fortran using parallel libs like MPI, Posix or even Open-MP. Any links and suggestions welcome. ...

How to make this code more functional ?

Hi I am a newbie in functional programming. I just tried solving the following problem : [ a rough specification ] e.g.1: dividend : {3,5,9} divisor : {2,2} radix = 10 ans (remainder) : {7} Procedure : dividend = 3*10^2+5*10^1+9*10^0 = 359 similarly, divisor = 22 so 359 % 22 = 7 e.g.2: dividend : {555,555,555,555,555,555,555,555,55...

How to store child objects on GAE using JDO from Scala

Hi, I'm have a parent-child relation between 2 classes, but the child objects are never stored. I do get an warning: "org.datanucleus.store.appengine.MetaDataValidator checkForIllegalChildField: Unable to validate relation net.vermaas.kivanotify.model.UserCriteria.internalCriteria" but it is unclear to me why this occurs. Already tri...

How can I iterate a list of lists in scala?

hello, I am trying to implement my own generic flatten for list objects which hold lists in scala. At this point I have def myFlatten[T](list: List[List[t]]): List[T] = { for (xs <- list) for (x <- xs) yield x } I am getting message for xs found Unit required list. ...

good code dojo question for introductory scala?

Hi folks--we're reading Programming Scala in our tech book club, and I'm thinking having a quick code dojo after the introductory chapters would be a good way to get our feet wet with the language before we proceed on. Does anyone have ideas for good programming problems that we could tackle in a 30-45 session that exercise basic Scala? ...

How can I add scala actors to an existing program without interfering with the normal termination behavior?

This program, after executing main(), does not exit. object Main { def main(args: Array[String]) { ... // existing code f() ... // existing code } def f() { import scala.actors.Actor._ val a = actor { loop { react { case msg: String => Syst...

How to limit concurrency when using actors in Scala?

I'm coming from Java, where I'd submit Runnables to an ExecutorService backed by a thread pool. It's very clear in Java how to set limits to the size of the thread pool. I'm interested in using Scala actors, but I'm unclear on how to limit concurrency. Let's just say, hypothetically, that I'm creating a web service which accepts "jobs"...

What is the difference between scala's case class and class

I searched in google to find the differences between the two, every one mentions that when you want to do pattern matching on the class use case class else use classes and also mentioning some extra perks like equals and hascode overriding. But are these the only reasons why one should use a case class instead of class? I guess there sho...

A generic quicksort in Scala

Hi, I've been playing around with Scala recently and was thinking about how to implement a generic version of quicksort in it (just to get a better feeling for the language) I came up with something like this object Main { def qs[T](a: List[T], f: (T, T) => Boolean): List[T] = { if (a == Nil) return a val (l, g) = a drop 1 ...

Scala: Best way to parse command-line parameters (CLI)?

What's the best way to parse command-line parameters in Scala? I personally prefer something lightweight that does not require external jar. Related: Java library for parsing command-line parameters? What parameter parser libraries are there for C++? Best way to parse command line arguments in C# ...

Looking for examples of how to use "@_*" when doing pattern matching in Scala

I have been searching for a bit but can not locate any examples that demonstrate the usage of @_* while pattern matching case classes. Below is an example of the kind of application I am referring to. def findPerimeter(o: SomeObject): Perimeter = o match { case Type1(length, width) => new Perimeter(0, 0, length, width) case Type2(rad...