scala

Can I transform this asynchronous java network API into a monadic representation (or something else idiomatic)?

I've been given a java api for connecting to and communicating over a proprietary bus using a callback based style. I'm currently implementing a proof-of-concept application in scala, and I'm trying to work out how I might produce a slightly more idiomatic scala interface. A typical (simplified) application might look something like thi...

How to define a ternary operator in Scala which preserves leading tokens?

I'm writing a code generator which produces Scala output. I need to emulate a ternary operator in such a way that the tokens leading up to '?' remain intact. e.g. convert the expression c ? p : q to c something. The simple if(c) p else q fails my criteria, as it requires putting if( before c. My first attempt (still using c/p/q as abo...

Generics and Constrained Polymorphism versus Subtyping

In this PDF presentation on Haskell Type Classes, slide #54 has this question: Open Question: In a language with generics and constrained polymorphism, do you need subtyping too? My questions are: How do generics and constrained polymorphism make subtyping unnecessary? If generics and constrained polymorphism make subty...

By-name repeated parameters

How to pass by-name repeated parameters in Scala? The following code fails to work: scala> def foo(s: (=> String)*) = { <console>:1: error: no by-name parameter type allowed here def foo(s: (=> String)*) = { ^ Is there any other way I could pass a variable number of by name parameters to the method? ...

Why is this Scala example of implicit parameter not working?

simple REPL test... def g(a:Int)(implicit b:Int) = {a+b} Why do neither of these attempted usages work? 1. scala> class A { var b:Int =8; var c = g(2) } :6: error: could not find implicit value for parameter b: Int class A { var b:Int =8; var c = g(2) } 2. scala> class A(var b:Int) { var c = g(2) } :6: error: could no...

What's the new way to iterate over a Java Map in Scala 2.8.0?

How does scala.collection.JavaConversions supercede the answers given here: http://stackoverflow.com/questions/495741/iterating-over-java-collections-in-scala (doesn't work because the "jcl" package is gone) and here http://www.eishay.com/2009/05/iterating-over-map-with-scala.html (doesn't work me in a complicated test which I'l...

Does the @inline annotation in Scala really help performance?

Or does it just clutter up the code for something the JIT would take care of automatically anyway. ...

Overloading generic implicit conversions

Hi I'm having a little scala (version 2.8.0RC1) problem with implicit conversions. Whenever importing more than one implicit conversion the first one gets shadowed. Here is the code where the problem shows up: // containers class Maybe[T] case class Nothing[T]() extends Maybe[T] case class Just[T](value: T) extends Maybe[T] case class...

How to combine Option values in Scala?

Hi! I want to be able to apply an operation f: (T,T) => T to Option[T] values in Scala. I want the result to be None if any of the two values is None. More specifically, I want to know if is there a shorter way to do the following: def opt_apply[T](f: (T,T) => V, x: Option[T], y: Option[T]): Option[T] = { (x,y) match { case (Som...

Difference between Array and List in scala

In what cases I should use Array(Buffer) and List(Buffer). Only one difference that I know is that arrays are nonvariant and lists are covariant. But what about performance and some other characteristics? ...

Improving MVP in Scala

The classical strongly typed MVP pattern looks like this in Scala: trait IView { } trait Presenter[View <: IView] { // or have it as an abstract type member val view : View } case class View1(...) extends IView { ... } case object Presenter1 extends Presenter[View1] { val view = View1(...) } Now, I wonder if there is any nice...

Why scala not allowing '$' identifier in case statement?

this works as expected scala> 3 match { case x:Int => 2*x } res1: Int = 6 why does this fail? scala> 3 match { case $x:Int => 2*$x } :1: error: '=>' expected but ':' found. 3 match { case $x:Int => 2*$x } ^ scala> 3 match { case `$x`:Int => 2*$x } :1: error: '=>' expected but ':' found. 3 matc...

Traits vs template

I m new in scala and i wonder what's the differences between traits and template ? When should i use template and when should i use traits ? thanks ...

Declaring functions two ways. What is the distinction?

Are these two function declarations effectively different? If not, why do they have different toString values? scala> def f: (Int) => Int = x=> x*x f: (Int) => Int scala> def f(x: Int) = x*x f: (Int)Int ...

Best method to peek into a Scala Actor's Mailbox

Using Scala 2.8 RC1 or newer, what is the best (easiest and/or most direct) method to "peek" at the waiting messages in an actor's mailbox (from within the same actor's act() method) in order to examine what is in the queue, without having to react/receive the messages and/or disturb the current contents of the mailbox in any way. The p...

Is Odersky serious with "bills !*&^%~ code!" ?

In his book programming in scala (Chapter 5 Section 5.9 Pg 93) Odersky mentioned this expression "bills !*&^%~ code!" In the footnote on same page: "By now you should be able to figure out that given this code,the Scala compiler would invoke (bills.!*&^%~(code)).!()." That's a bit to cryptic for me, could someone explain what's go...

Cross product of 2 sets in Scala

val cross = (for (x<-setA; y<-setB) yield (x,y)) val cross2 = (setA flatMap (x => setB map ((x,_))) Is there a more elegant way to do this with a cross product operator, or some such? E.g: val cross3 = setA cross setB ...

TextField Listener

Hi, there's a swing JTextField, and I want to add a listener, so whenever the users types a single letter, there's an event. There's a ValueChanged event in scala api, but I don't get it what's it's peer. So which one Listener should I use? KeyListener and implement reasonably just one method? ...

Using Scala structural types with abstract types

I'm trying to define a structural type defining any collection that has an "add" method (for instance, a java collection). Using this, I want to define a few higher order functions that operate on a certain collection object GenericTypes { type GenericCollection[T] = { def add(value: T): java.lang.Boolean} } import GenericTypes._ tra...

Catch all exceptions in Scala 2.8 RC1

I have the following dummy Scala code in the file test.scala: class Transaction { def begin() {} def commit() {} def rollback() {} } object Test extends Application { def doSomething() {} val t = new Transaction() t.begin() try { doSomething() t.commit() } catch { case _ => t.rollback() } } If I compile...