Archive for October, 2008

Intellij + Scala; it is with great regret…

Thursday, October 30th, 2008

Sadly, despite my expressed hopes, in appears the state of Intellij IDEA with Scala is not improving but worsening. This is sad. Each time you start up IDEA you still get a lovely internal error and exception stack trace. But worse, ever since the 8890 EAP build, the application hangs with any reasonably sized Scala project. This is even more sad. A colleague has confirmed this on his Mac, however, we are unable to revert to a usable version since you cannot download older EAP releases.

The suspected trend toward becoming more unusable for Intellij IDEA and Scala is regrettably confirmed. I wish it were not so ):

The Windows Vista upgrade to XP effect perhaps?

Java/Ruby does not generalise to static/dynamic

Thursday, October 16th, 2008

So you were once a Java fanboy and now you’ve apostatised to the Ruby cult. Now you believe you hold veritable opinions about “statically typed languages” and “dynamically typed languages”. How wrong you are.

Java is not representative of static type systems, not even a bit. It lacks some of the most basic features of a static type system. It might even be said that its static type system imposes some of the most impractical insanity that could possibly be conceived. Why do I think this? Because I have pushed Java’s static type system to its limits and relative to its peers, it fails miserably. I’ve seen the language inside-out. I used to work on the JDK implementation for a certain corporation (of questionable ethical standards). I even have the spanky Sun Java Programmer/Developer certifications. Yes I have been drowned in Java Jolly Junkie bullshit and revived.

So you use Ruby eh? It allows you to express things that Java was too rigid to allow, right? That doesn’t mean a thing about static type systems, right? RIGHT?

When you use Java and Ruby as the example for static and dynamic type systems, I am reminded of my children who compare the flight of their scrunched up pieces of paper… er highly aerodynamic, radar-resistant aircraft (sorry boys). Hopefully some day they will learn what a wind tunnel is and what it takes to engineer a fighter-aircraft. Will you?

This kind of enthusiasm is to be encouraged — if you are 6 years old — but you are not, so you just look like you are in an adult’s body. No really, you do. This might be fine with you (who am I to judge?), but if it is not, it might help to know What to Know Before Debating Type Systems. Just a thought — there is plenty of other information available should you desire to seek it, just drop me a line.

Here are some tips to get started; static typing and explicit type annotating are two very different things. Don’t use the two notions interchangeably — not even loosely. Dynamically-typed languages do not produce shorter code, this is a myth. In fact, what constitutes shorter code is often misunderstood — certainly by Ruby (and Python, Groovy for that matter) advocates that I have encountered. I’m tempted to dispense with this myth with a challenge, but I’m not sure if I can be bothered (I have a life outside of whinging on my blog you know). It would also fail to do the topic justice. An entire discussion on what constitutes shorter code is in order.

For the love of GADTs, in the meantime, please stop talking nonsense about static and dynamic type systems. It only serves to hurt other potential learners by perpetuating misinformation. Myths spread by repetition — be honest and break the cycle.

Does Scala have Java’s ternary operator?

Monday, October 13th, 2008

I hear this question a lot. Yes it does. Instead of c ? p : q, it is written if(c) p else q.

This may not be preferable. Perhaps you’d like to write it using the same syntax as Java. Sadly, you can’t. This is because : is not a valid identifier. Fear not, | is! Would you settle for this?

c ? p | q

Then you’ll need the following code. Notice the call-by-name (=>) annotations on the arguments. This evaluation strategy is required to correctly rewrite Java’s ternary operator. This cannot be done in Java itself.

case class Bool(b: Boolean) {
  def ?[X](t: => X) = new {
    def |(f: => X) = if(b) t else f
  }
}
 
object Bool {
  implicit def BooleanBool(b: Boolean) = Bool(b)
}

Here is an example using the new operator that we just defined:

object T {
  val condition = true
 
  import Bool._
 
  // yay!
  val x = condition ? "yes" | "no"
}

Have fun ;)

Project Euler Problem 2 Functional Java

Friday, October 3rd, 2008

Project Euler Problem 2 using Functional Java:

import fj.data.Stream;
import fj.P1;
import fj.F2;
import static fj.function.Integers.even;
import static fj.pre.Ord.intOrd;
import static fj.pre.Monoid.intAdditionMonoid;
import static fj.data.Stream.cons;
import static fj.Function.curry;
 
...
 
Stream<Integer> fibs = new F2<Integer, Integer, Stream<Integer>>() {
  public Stream<Integer> f(final Integer a, final Integer b) {
    return cons(a, P1.curry(curry(this).f(b)).f(a + b));
  }
}.f(1, 2);
 
final int problem2 = intAdditionMonoid.sumLeft(fibs.takeWhile(intOrd.isLessThan(1000001)).filter(even).toList());

Project Euler Problem 1 Functional Java

Friday, October 3rd, 2008

Project Euler Problem 1 using Functional Java:

import static fj.pre.Monoid.intAdditionMonoid;
import static fj.data.List.range;
import fj.F;
 
...
 
final int problem1 = intAdditionMonoid.sumLeft(range(0, 1000).filter(new F<Integer, Boolean>() {
  public Boolean f(final Integer a) {
    return a % 3 == 0 || a % 5 == 0;
  }
}));

Scala: Gotchya!

Wednesday, October 1st, 2008

I have been caught out by type-checking code that fails at runtime. It should have failed at compile-time due to a language limitation in structural types — not a logical absurdity in the code itself. Thankfully I have a workaround, but it does provoke persistent hesitance — giving that disgusting feeling as if I were using something as degenerate as, for example, Ruby or even worse (can it get worse?), Groovy. I’ll have to counsel myself on that one.

Nevertheless, I give warning. Here is the bug report. Here is the fun. Observe:

scala> def left[B] = new { def apply[A](a: A): Either[A, B] = Left(a) }
left: [B]java.lang.Object{def apply[A](A): Either[A,B]}
 
scala> left[String](7)
res6: Either[Int,String] = Left(7)
 
scala> def left[B] = new { def apply[A](a: A, b: B): Either[A, B] = Left(a) }
left: [B]java.lang.Object{def apply[A](A,B): Either[A,B]}
 
scala> left(7, "")
res8: Either[Int,java.lang.String] = Left(7)
 
scala> left[String](7, "")
java.lang.NoSuchMethodException: $anon$1.apply(java.lang.Object, java.lang.String)
        at java.lang.Class.getMethod(Class.java:1605)
        at .reflMethod$Method1(<console>:6)
        at .<init>(<console>:6)
        at .<clinit>(<console>)
        at RequestResult$.<init>(<console>:3)
        at RequestResult$.<clinit>(<console>)
        at RequestResult$result(<console>)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native ...