Sunday, October 5, 2008

Collection Filtering in BGGA

As a follow-on to last week's post, I decided to see what my collection filtering example would look like using the draft BGGA proposal. So, I downloaded the feature-complete prototype, and this is the working example I came up with:

package com.test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.google.common.base.Join;

public class BGGAFilter<T> {
  
  public static void main(String[] args) {
    List<Integer> ints = new ArrayList<Integer>();
    for( int i = 1; i <= 10; i++)
      ints.add(new Integer(i));
    BGGAFilter<Integer> filter = new BGGAFilter<Integer>();
    List<Integer> filtered = filter.filter(ints, { Integer x => x % 2 == 0 });
    System.out.println("(" + Join.join(", ", filtered) + ")" );
  }

  public List<T> filter(List<T> in, { T => boolean } predicate) {
    List<T> out = new ArrayList<T>();
    for( T item : in)
      if( predicate.invoke(item))
        out.add(item);
    return out;
  }
}




This is not a completely apples-to-apples comparison with my previous Java example using the Google Collections API. You'll notice that in the above example, I had to implement the filter method, whereas the google collections API provides it for us. Now that I've seen examples in Scala, Java, and Java + BGGA, I couple of questions come to mind:

  1. Is the BGGA example 'better' than the straight Java example? If you think of the language itself as an API, and programmers as users of your API, then you start thinking in terms of usability, simplicity, power and self-documenting code. That is, many (but not all) of Josh Bloch's bumper sticker API design principles apply. The BGGA example carries much less syntactic weight than the straight Java example. The former focuses directly on what the code is doing, whereas the latter devotes many more characters to how the code does the job. Following Josh Bloch's advice to "keep APIs free of implementation details," I would choose BGGA over straight Java. This is by no means a comprehensive comparison, but rather a shard of evidence in the Java closures debate. The other thing to consider, of course, is that I am not even comparing FCM and CICE, two other leading Java closures proposals. Based on a very cursory evaluation, I have chosen to hone in on BGGA.
  2. If the use of closures and anonymous functions is instrumental in my problem space, why not just use a language that supports functional programming, such as Scala? If you need true functional programming now, then the answer is simple. Use a functional language. On the hypothetical side, suppose BGGA makes it into Java, and you just need closures. This one is more difficult for me to answer. You need to assess: What is the talent on my team? How much time do I have to complete the project (do we have time to learn)? Try and come up with one or more use cases that would demonstrate why one would be better than the other. For me, the jury's still out. I'm heartened by the fact that Scala is built on the JVM, and has tight integration with existing Java libraries. I believe this will certainly help its adoption.

Saturday, September 27, 2008

Java vs. Scala - an example from the Google Collections API

As a Java developer, I've heard a lot of hubub, and some would say hype about Scala, so I decided to see what it has to offer. Scala is a multi-paradigm general purpose language, supporting both object-oriented and functional styles. As I learn Scala, I hope to also gain insight into the relative strengths and weaknesses of both languages. This is my first attempt at comparing the two.

Being somewhat familiar with the Google Collections API, I decided to do a simple side-by-side comparison between filtering a collection in Java (using Google collections) versus Scala. Below is the complete Java example, which takes a collection of integers from 1 to 10 and filters them down to only the even numbers:


package com.test;

import java.util.ArrayList;
import java.util.Collection;

import com.google.common.base.Join;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;

public class GoogleCollectFilter {

public static void main(String[] args) {
Collection<Integer> ints = new ArrayList<Integer>();
for( int i = 1; i <= 10; i++)
ints.add(new Integer(i));
Collection<Integer> filtered =
Collections2.filter(ints, new Predicate<Integer>() {

@Override
public boolean apply(Integer arg) {
return arg.intValue() % 2 == 0;
}});

System.out.println("(" + Join.join(", ", filtered) + ")" );
}
}


Here now is the equivalent code in Scala:


package com.test

object ScalaFilter {

def main( args : Array[String] ) = {
println((1 to 10).filter((n : Int) => n % 2 == 0))
}

}


If one were to judge purely by verbosity, Scala definitely wins this round. Even with Google's well-designed API, there are still many syntatic hoops that one must go through to do a simple filtering on a collection, due to current limitations in the Java language. Now I'm curious to find an example where the Java code is cleaner. Stay tuned...

Friday, August 22, 2008

Java-Java-jing-jing-jing

I just had to share this. I'm a huge fan of the Java Posse podcasts, and I wanted to find out where the theme song came from. It turns out that it was a WWI song called ja-da, with the lyrics re-written. I found the original audio of ja-da, and it's interesting to compare with the Java Posse version.

Sunday, July 6, 2008

GTD with Quicksilver and Gmail

Getting Things Done (GTD) is a methodology, authored by David Allen, for, well, getting things done with minimal stress and maximum efficiency. It worked really well for me when I learned about it a couple of years ago, but I fell off the horse and I've recently started getting back into it. I have been hunting for a GTD tool with the following characteristics:

  1. I can get to it from "anywhere" (i.e. it's web-based)
  2. It has a nice mobile version
  3. It has great desktop integration so I can literally manage my action items with a few keystrokes
  4. It's free (or practically)
So far, I haven't found a tool that satisfies me on all of the above fronts. I have extensively test-driven Remember The Milk (RTM) and Vitalist, but I don't like having to pull up my browser, go to the site and navigate the UI with my mouse to enter a quick action item as I think of it. I am a huge fan of Quicksilver (i.e. I'm a Mac user), and I love being able to rapidly carry out various actions with a few keystrokes, without having to switch windows.

With that in mind, I have figured out a way to quickly add tasks to my RTM or Vitalist inbox with a few keystrokes. I wouldn't go so far as to call it a "hack," but it's a gem nonetheless.

Prerequisites
  1. Sorry PC/Linux users - this will only work on a Mac with OS X Leopard (it might work with earlier OS X versions, but I can't attest either way).
  2. The instructions below should work with any web-based task tool that has an email address for the task inbox. I know that RTM and Vitalist both have email addresses, and probably others do as well.
  3. Quicksilver with Apple Mail Module installed (lifehacker has an article showing how to use it). If you use Gmail, make sure you install the Gmail module as well
The Step-by-Step
  1. Get the email address of your task inbox and add it as an entry in address book (give it an easy to remember nickname)
  2. Invoke Quicksilver.
  3. Type a "." and start entering your task description; hit tab and type "email." Then hit tab again and enter the nickname of the address book contact you entered. Hit "enter"
  4. This will automatically send the message if you are in apple mail, or open up a "compose" screen in your browser if you use gmail. If the latter is the case, simply hit tab, then enter to send the message to your task list.

That's it! Hopefully the time you spent reading this short post will be more than made up in time saved entering tasks. ;-)