News: Bringing Fun to the Java Platform? About fun4j

  1. Bringing Fun to the Java Platform? About fun4j (6 messages)

    Fun4j aims at bringing concepts from functional programming to the JVM.
    It provides developers with a Java API for using functional programming
    techniques like anonymous functions, higher order functions and closures.
    (see api tutorial for an introduction)

    Functions can be defined as Java anonymous classes, or alternatively and
    more elegantly, in a functional subset of LISP. LISP- and Java-functions are fully interoperable.
    (see java-lisp integration for a short tutorial)

    Thus fun4j also comes with a complete lambda-term to Java bytecode compiler and
    a small REPL shell. Bytecode is generated on the fly with the superfast ASM library.
    Thanks to some simple optimization techniques like tail code optimization (TCO) the compiler produces code that runs pretty fast.
    (see the lisp repl for a short intro) ?

    Threaded Messages (6)

  2. How does this compare to Clojure (lisp-based) or Scala (some functional features)? Why yet another functional language for the JVM? :)

  3. It seems to be an api rather than a language. so it's more in the line of op4j, commons functor etc

  4. Bringing more Fun to Java[ Go to top ]

    fun4j could be called a hybrid between functional Java APIs (like functionaljava, commons functor, lambdaj) and functional languages for the JVM like clojure and scala.

    on the Java API side you can do thinks like the following:

            Collection col = fun4j.asCollection(1 , 2, 3, 4, 5, 6, 7, 8, 9, 10);
            Function add = new Function() {
                public Object apply(Object... args) {
                    return (Integer) args[0] + (Integer) args[1];
            Integer sum = (Integer) fun4j.foldright(add, 0, col);

    This looks similar to examples from other functional APIs.

    But fun4j also provides a LISP compiler, so you can also define your functions in classic Lisp syntax:

            Function add = fun4j.compile("(lambda (x y) (+ x y))");
            Integer sum = (Integer) fun4j.foldright(add, 0, col);

    And finally fun4j also comes with a complete interactive Lisp shell (REPL).
    The actual language is a functional subset of LISP, comparable to Lispkit.

  5. Another well regarded library along these lines is functionaljava; http://functionaljava.org/

  6. Closures[ Go to top ]

    The section about 'Closures' in your api tutorial confuses Closures with Curryin(or at least partial application).

    A Closure is a Function which refers to a free variable that is not declared as a formal argument in the argument list of that Function. In that sense, the Function definition forms an 'Open Term'. In Order to get to an 'Closed Term', the Compiler 'closes over' to the lexical scope (in which the function was defined) and tries to bind the free variable.

    Your example of Function 'add' isn't considered an Open Term, since all of its variables (used within the Function body) are defined as formal Arguments of that Function.

    Having said that, your 'bind' looks rather like a 'partial application' of the Functions arguments which results in a Function of arity 0. But that's nothing to do with Closures ...

  7. Closures[ Go to top ]

    Hi Mario,

    You are right. I'll correct that part of the tutorial.

    thanks for your input!