Wednesday, January 9, 2008

Functional constructs Java 7, Groovy, Commons

What functional constructs to use for ShapeLogic

I have started to code the lazy streams for ShapeLogic. They are going to be key for the query interface to ShapeLogic. I need some functional constructs to work with these. The top sources candidates are:
  1. Apache Commons
  2. Groovy
  3. Java 7
  4. Hand coding
None of these are a perfect fit. This post list some of the advantages and disadvantages of these.

Apache Commons functional constructs

I am currently using Apache Commons JEXL for user expressions.
  1. Apache Commons do not use templates
  2. Apache Commons Functor are still in the sandbox and not actively developed
  3. The code is not uniform
  4. You cannot make user define functions in Apache Commons JEXL

Groovy functional constructs

I need to use a scripting language, to define user functions.
  1. Groovy contains all of Java's constructs
  2. Groovy comes with good functional constructs
  3. I like the Groovy syntax for using these
  4. I cannot use these expressions directly since Groovy is not a lazy language

Java 7 functional constructs

Java 7 comes with good functional constructs.
  1. There are a lot of interest for functional constructs in Java 7
  2. I would rather use Java 7 than compete with it
  3. Java 7 still seem to be pretty far away
  4. It is not sure that closures will make it into Java 7

Thoughts on immutable constructs

I will probably make the convention that a lazy stream like the Fibonacci numbers are immutable, but not enforce this by making a LISP list.

Scala envy

Now I suffer from Scala language envy. These Scala language features would come in handy now:
  1. Lazy streams
  2. List comprehension, with same syntax for lists, iterators and streams
  3. First class function
  4. Closures
  5. Lazy calculation
  6. Uniform access to functions and lists
I could of cause try to include the scala.jar and directly use the Scala constructs in ShapeLogic, but there lay the road to madness.

Hand coding functional constructs

This might not be too much work, but I would rather not create yet another implementation of functional constructs. Apache Commons Functor never took off, despite some good press. That is probably an indication that Java was not that well suited for elegant functional constructs when this library was made, and maybe still isn't.

Current work plan

  1. I will start by implementing a lazy Fibonacci stream
  2. Then I will move the polygon finder to a lazy stream, before it could only find one polygon
I will try to get ShapeLogic 0.9 out soon, even if it is a very limited version.

If you know of any libraries that would fit my need and not be too heavyweight, please let me know.

Two Fibonacci implementations

The Haskell language has an incredibly elegant lazy stream implementation:
fibs :: [Int]
fibs = 1 : 1 : [ a + b | (a, b) <- zip fibs (tail fibs)]

Drools Fibonacci implemented. This is not a good fit for my computer vision project.

-Sami Badawi

1 comment:

Alex Miller said...

Other options:

- FunctionalJava
- Clojure

It's unlikely I think that closures will be in Java 7.