Monday, June 9, 2008

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.

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.

if you had really read the documentation on collection utils, you would have found the method "collect" which

Returns: the transformed result (new list)

Seems like a deliberate attempt to write obfuscated code to come to a conclusion that you had decided on prior to writing any code. Clearly drinking the cool aid without knowing what is put into it. Rather special...

Indeed, on the comment just before yours, I do mention that:"Just a detail I forgot to mention previously: I have to use jdk 1.4.2 at work. so that's why I am not using generics and all."

About the "collect" function, thank your for the pointer. I had skimmed through the documentation too quickly.

I really did not set out to write obfuscated code. I was just honestly documenting an attempt to try out a more functional style in java 1.4. No doubt a more experienced person than me would have gotten a better result.