Tuesday, June 17, 2008

Scala Light... A Java successor?

Upon reading on a few forums in Artima, I detected a desire for both a fuller Java and a simpler Scala. A fuller Java to have things like type inference and more functional style constructs. A simpler Scala to be have an easier specification, both for the mind and for the IDE.

What do you think would be more appropriate to help Java developers get a better language?
  • Continue to selectively better the Java Language
  • Define a subset of Scala, a Scala Light if you will. Or would a subset of Scala defeat the purpose of its synergy of features?
  • Another choice

Tuesday, June 10, 2008

A simple Java FP list map

Following on the comments I received on my previous entry about doing a bit of Java functional programming style, I made a very simple class to map a list. I'm following the style of what I have seen in the apache collections utilities. See previous post.

First note that I am doing it without generics (I'm stuck with jdk1.4.2 at work), so for some this would be old style Java programming.

Here's the utility class.
ListUtils.java

package fp;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ListUtils {
static public List map(List elements, Applier applier) {
List result = new ArrayList();
for (Iterator it = elements.iterator(); it.hasNext();) {
Object element = it.next();
result.add(applier.apply(element));
}
return result;
}
}


And the interface used by the utility class.
Applier.java

package fp;

public interface Applier {
public Object apply(Object element);
}


Here's a client code example.

WithPoints.java

package domino;

public interface WithPoints {
public Integer getPoints();
}


Domino.java

package domino;

public class Domino implements WithPoints {
private Integer points;

public Domino(int points) {
this.points = new Integer(points);
}

public Integer getPoints() {
return points;
}
}


And finally!

package domino;

import fp.Applier;
import fp.ListUtils;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Main {

public static void main(String[] args) {
List dominoes = new ArrayList();
dominoes.add(new Domino(1));
dominoes.add(new Domino(2));
dominoes.add(new Domino(3));

List points = ListUtils.map(dominoes, new Applier() {
public Object apply(Object domino) {
return ((WithPoints) domino).getPoints();
}
});

System.out.println(points); // [1, 2, 3]
}
}


So this time around, I did get a new list instead of transforming a list but I still think the simple iterator style is clearer and it avoids using a "utility class" for doing something really simple.

Iterator style

List points;
for (Iterator iter = Dominoes.iterator(); iter.hasNext();) {
points.add((WithPoints) iter.next().getPoints());
}


And of course, Scala still is neater for me 8)

val points = dominoes map (x => x.points)

Monday, June 9, 2008

The Scala Influence

A while ago, Frank Sommers asked the following:
How Has Functional Programming Influenced Your Coding Style?

If wish I could have replied something then, but I was just starting to learn Scala, and I couldn't.

Now, I'm still just at the beginning of grokking FP, but I can write about how it almost influenced a bit of Java code I had to write the other day at work.

Post update: "I have to use jdk 1.4.2 at work. That's why I am not using generics and all."

I had a list of instances of a certain class, and I wanted to get a list made from the result of invoking a certain method on each instance.

We'll pretend I had a Domino class, and I wanted to invoke the method getPoints() on each instances

Here's how I would have done it in Scala:

class Domino(p: int) {
val points=p
}

val dominoes = List(new Domino(1), new Domino(2), new Domino(3))

val points = dominoes map (_.points)

Let's say it's a bit cryptic in Scala and make it a bit more explicit:
val points = dominoes map (x => x.points)

I'll skip the Java class definition, but it implements the interface WithPoints, which declares the "Integer getPoints()" method .

Now, just look at what I had to code in Java to get the point list:

List points;
for (Iterator iter = Dominoes.iterator(); iter.hasNext();) {
points.add((WithPoints) iter.next().getPoints());
}

Simple really, but I couldn't get over how much neater (to me) it was in Scala.

Now comes the functional programming influence part. It so happens that I had a transformation library at my disposal in my Java project: org.apache.commons.collections

It's not what I really wanted, but I could use some kind of functional programming style with it. And here's the end result:

List dominoes;
CollectionUtils.transform(dominoes, new Transformer() {
public Object transform(Object domino) {
return ((WithPoints) domino).getPoints();
}
});


So there, it's not so bad, except that:
1) It transforms my list instead of returning a new list, which is what I wanted.
2) The imperative version is, in my opinion, clearer.

And so this is how functional programming has (almost, but not quite) influenced my coding style so far.