Mario has posted 14 posts at DZone. You can read more from them at their website. View Full User Profile

Is it Time for Functional Programming in Java?

09.14.2009
| 12097 views |
  • submit to reddit

Fuctional programming has existed for decades, but only in the last few years has it gained the attention it deserves, for a lot of very good reasons like:

  • it reduces code duplicarion
  • it improves readability
  • it allows better reuse
  • it facilitates multi-threading programming
  • it eliminates ugly programming by side-effects

Unfortunately Java doesn't natively allow you to take advantage of functional programming. It seemed for a couple of years that Java 7 was going to fill this lack by implementing the BGGA specification. But in the end they decided to keep BGGA out of Java 7, disappointing for the functional programming fan. This is one of the reasons why many Java programmers are migrating (or at least evaluating to migrate) toward more modern JVM compatible languages like Groovy and Scala. These languages allow developers to leverage the features of both object oriented and functional programming without losing the reliability and portability provided by a rock-solid and mature runtime environment like the JVM one.

Anyway, if you don't want to learn a completely new language (even if there could be some good reasons to do that as outlined  here) or more likely you are not allowed to use something different to Java, you could still introduce some functional programming techniques in your code. Indeed there are some tools and libraries available that allow you to do that in Java.

Probably the best known of these library is functionalJava which offers the possibility to write code following the BGGA specification already in Java 6. However this solution has, in my opinion, some important drawbacks like the needs to preprocess your code in order to translate the BGGA statements and the complete lack of support for the BGGA syntax in  IDEs.

Another tool that could serve to this purpose is functionalj. It has the advantage of not requiring any code translation, but it also looks far less powerful than functionalJava and doesn't seem to offer a satisfying generics support.

In the end, with its 2.0 release, lambdaj has provided a third option for functional programming in Java by allowing developers to define and use first-class functions, in a more readable DSL style. It seems to combine the pros of both the former libraries, but at the same time it presents some important limitations due to the heavy use of proxy made in its implementation.

To summarize if, like me, you feel the absence of functional programming in Java, these tools, despite not being perfect, can be a valuable replacement while waiting to finally see those features that will be available natively in plain Java.
Published at DZone with permission of its author, Mario Fusco.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

David Walend replied on Sun, 2009/09/27 - 3:39pm

mwildam, Solid question. We're interested in it because we're going from simulated multithreaded systems to (now) real multithreaded systems to (very soon) huge numbers of compute cores. Multithreaded shared memory code is very hard to write correctly. Things we've done for our whole careers to write efficient code no longer make the most efficient code because those things fail to use all those cores. Functional programming, despite its odd feel, does make it easy for us to use all those cores.

Martin Wildam replied on Fri, 2009/10/16 - 7:37am

Yes, it is difficult to correctly deal with data accessed by multiple threads or even processes.

But I was mentioning to use static methods and not shared data - I think there is a difference. You can have static methods and just pass them the (thread-specific) objects holding the appropriate data.

Borna Novak replied on Sun, 2012/01/08 - 10:45am in response to: Martin Wildam

Any language can be coerced to do anything, that's what Turing completeness is all about, the thing that separates them is how easily some thing can be implemented, maintained and changed.

 Functional programming offers a natural  paradigm for parallelisation, but also offers elegant patterns to a large number of common tasks like array transformations

> array = [1, 2, 3, 4]
=> [1, 2, 3, 4]
> array.map{­|x| x + 40}
=> [41, 42, 43, 44]
> array.sele­ct{|x| x.odd­?}
=> [1, 3]
 
 and wrapper patterns, like the exception handling pattern: 
def log_and_go
  yield # code in curly braces will be called here
rescue Exception e
  send_alert e
  log_exception e
end

log_and_go {
  # stuff, any exception raised here will be alerted and logged
}
 Coming back to java after a while in an OOP language with functional elements makes you miss these constructs.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.