Showing posts with label Google Collections API. Show all posts
Showing posts with label Google Collections API. Show all posts

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...