scala

Case class to map in Scala

Does anyone know if there is a nice way I can convert a Scala case class instance, e.g. case class MyClass(param1: String, param2: String) val x = MyClass("hello", "world") Into a mapping of some kind, e.g. getCCParams(x) returns "param1" -> "hi", "param2" -> "3" Which works for any case class, not just predefined ones. I've found ...

Sorting objects based on Double values?

Sorting objects is simple enough by mixing in Ordered and providing a compare() function, as shown here. But what if your sorting value is a Double instead of an Int? def compare(that: MyClass) = this.x - that.x where x is a Double will lead to a compiler error: "type mismatch; found: Double required: Int" Is there a way to use Do...

Scala Traits Usage

Can someone give explanation of Scala traits? I can't seem to find one anywhere. Also, what are the advantages of traits over extending an abstract class? ...

scala dot syntax (or lack thereof)

I was going through the wonderful book, Programming in Scala when I came across a piece of code that just doesn't make sense to me: def above(that: Element): Element = { val this1 = this widen that.width val that1 = that widen this.width elem(this1.contents ++ that1.contents) } Note line 2 and 3: val this1 = this widen th...

Preferred way to create a scala list

There are several ways to construct an immutable list in scala (see contrived example code below). You can use a mutable ListBuffer, create a var list and modify it, use a tail recursive method, and probably others that I don't know about. Instinctively, I use the ListBuffer but don't have a good reason for doing so. Is there a pref...

What are the biggest differences between Scala 2.8 and Scala 2.7?

I've written a rather large program in Scala 2.75, and now I'm looking forward to version 2.8. But I'm curious about how this big leap in the evolution of Scala will affect me. What will be the biggest differences between these two versions of Scala? And perhaps most importantly: Will I need to rewrite anything? Do I want to rewrite a...

Is there anything like Haskell's 'maybe' function built into Scala?

What I'm looking for is this function: def maybe[A, B](a: Option[A])(f: A => B)(g: () => B): B = a match { case Some(x) => f(x) case None => g() } It's in the Haskell prelude so I'm thinking it might be in the Scala standard library somewhere and I've just missed it. I hate having to recode it in projects so I'm wondering if ...

Lift: how to retrieve the current logged in user?

I'm starting to learn Lift and I'm stuck. I have problem with simple snippet: class Util { def in(html: NodeSeq) : NodeSeq ={ if (User.loggedIn_?) Helpers.bind("user", html, "name" -> User.currentUser.map(_.lastName).open_!) else NodeSeq.Empty } It should inject current User name, but ...

Practical use of futures? Ie, how to kill them?

Futures are very convenient, but in practice, you may need some guarantees on their execution. For example, consider: import scala.actors.Futures._ def slowFn(time:Int) = { Thread.sleep(time * 1000) println("%d second fn done".format(time)) } val fs = List( future(slowFn(2)), future(slowFn(10)) ) awaitAll(5000, fs:_*) println(...

scala / lift example of form processing

Can I get a simple example of a Lift form submission and processor? I've seen the Lift hello world, which was pretty trivial and didn't really give a flavor for how it works. I'm coming from a spring MVC background, but I have some FP experience. ...

Method Dependencies and Error Handling

I'm making a small issue management system in Lift to learn both Scala and Lift. I have a view that displays a single issue belonging to a project. Before I bind data to the view template, I want to check that I have all data required, so I want to specifically check that: A project ID param has been supplied A Project exists with the...

Scala actors: receive vs react

Let me first say that I have quite a lot of Java experience, but have only recently become interested in functional languages. Recently I've started looking at Scala, which seems like a very nice language. However, I've been reading about Scala's Actor framework in Programming in Scala, and there's one thing I don't understand. In chapt...

Scala: How to define "generic" function parameters?

I am trying to learn Scala now, with a little bit of experience in Haskell. One thing that stood out as odd to me is that all function parameters in Scala must be annotated with a type - something that Haskell does not require. Why is this? To try to put it as a more concrete example: an add function is written like this: def add(x:Doub...

How to define a cyclic type definition?

This is not a valid type definition: scala> type Addable = { def +(subject: Addable) } <console>:4: error: illegal cyclic reference involving type Addable type Addable = { def +(subject: Addable) } Can this be expressed in scala? ...

Calling an blocking Actor from within an Actor

Given I invoke an actor from inside react does this block the calling Actor or is it still processing other requests? class worker extends Actor() { def act() = { loop { react { msg => var foo = another_actor !? 'bar' //Block here? println(foo) } } } ...

How to use Java Collections.shuffle() on a Scala array?

I have an array that I want to permutate randomly. In Java, there is a method Collections.shuffle() that can shuffle the elements of a List randomly. It can be used on an array too: String[] array = new String[]{"a", "b", "c"}; // Shuffle the array; works because the list returned by Arrays.asList() is backed by the array Collections.s...

Programmatically setting Repeated Parameters in Scala

I'm trying to call Futures.awaitAll with a variable number of well...Futures. awaitAll is defined as awaitAll(timeout : Long, fts : Future[Any]*). I have tried passing in a List and an Array but both won't work: list = future1 :: future2 :: Nil Futures.awaitAll(1000, list) found : List[scala.actors.Future[Any]] required: scala.actors....

Scala: how to merge a collection of Maps

I have a List of Map[String, Double], and I'd like to merge their contents into a single Map[String, Double]. How should I do this in an idiomatic way? I imagine that I should be able to do this with a fold. Something like: val newMap = Map[String, Double]() /: listOfMaps { (accumulator, m) => ... } Furthermore, I'd like to handle ...

Does Scala AnyRef.clone perform a shallow or deep copy?

In Scala, does AnyRef.clone perform a shallow or deep copy? ...

What is the easiest way to deeply clone (copy) a mutable Scala object?

What is the easiest way to deeply clone (copy) a mutable Scala object? ...