Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Functional Programming in Java with Generics and CGLib

Functional Programming in Java with Generics and CGLib

Those interested in Functional Programming usually have to use a well suited-language like Scheme, Haskell, Ruby, or Groovy; or, in Java, use Anonymous Inner/Local classes to fake it like commons-collections. Ray Cromwell has a method for doing FP in Java using Generics and CGLib, reducing overhead and preserving type information for better use in IDEs integration:
Our goal is to try and make writing functions as easy as possible, and allow them to be curried, composed, and passed as arguments to other functions. Functions, as used in this article, are not just any old Java method. They are Java instance methods which are pure-functional, that is, without side-effects, and do not reference any object fields or non-static variables. Essentially, they are non-mutating static functions declared without a static qualifier.
In the essential read Why Functional Programming Matters, John Hughes argues that functional programming significantly improves modularity, which is a key to successful programming. FP creates a natural framework for developing small and simple modules which are then glued together.

IBM DeveloperWorks also published an article a couple of years ago showing how to do FP in Java, and Joel Spolsky of 'Joel on Software' has lamented the fact that all-Java universities these days are not preparing programmers by teaching them a functional language "Without understanding functional programming, you can't invent MapReduce, the algorithm that makes Google so massively scalable. The terms Map and Reduce come from Lisp and functional programming."

Rate this Article