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) ?
-
Bringing Fun to the Java Platform? About fun4j (6 messages)
- Posted by: Thomas Mahler
- Posted on: August 25 2010 11:05 EDT
Threaded Messages (6)
- Bringing Fun to the Java Platform? About fun4j by Andres F. on August 25 2010 12:57 EDT
- Bringing Fun to the Java Platform? About fun4j by jelmer kuperus on August 25 2010 14:46 EDT
- Bringing more Fun to Java by Thomas Mahler on August 26 2010 03:27 EDT
- Bringing Fun to the Java Platform? About fun4j by jelmer kuperus on August 25 2010 14:46 EDT
- Bringing Fun to the Java Platform? About fun4j by Michael Campbell on August 25 2010 22:09 EDT
- Closures by Mario Gleichmann on August 27 2010 02:49 EDT
- Closures by Thomas Mahler on August 27 2010 10:49 EDT
-
Bringing Fun to the Java Platform? About fun4j[ Go to top ]
- Posted by: Andres F.
- Posted on: August 25 2010 12:57 EDT
- in response to Thomas Mahler
How does this compare to Clojure (lisp-based) or Scala (some functional features)? Why yet another functional language for the JVM? :)
-
Bringing Fun to the Java Platform? About fun4j[ Go to top ]
- Posted by: jelmer kuperus
- Posted on: August 25 2010 14:46 EDT
- in response to Andres F.
It seems to be an api rather than a language. so it's more in the line of op4j, commons functor etc
-
Bringing more Fun to Java[ Go to top ]
- Posted by: Thomas Mahler
- Posted on: August 26 2010 03:27 EDT
- in response to jelmer kuperus
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. -
Bringing Fun to the Java Platform? About fun4j[ Go to top ]
- Posted by: Michael Campbell
- Posted on: August 25 2010 22:09 EDT
- in response to Thomas Mahler
Another well regarded library along these lines is functionaljava; http://functionaljava.org/
-
Closures[ Go to top ]
- Posted by: Mario Gleichmann
- Posted on: August 27 2010 02:49 EDT
- in response to Thomas Mahler
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 ...
-
Closures[ Go to top ]
- Posted by: Thomas Mahler
- Posted on: August 27 2010 10:49 EDT
- in response to Mario Gleichmann
Hi Mario,
You are right. I'll correct that part of the tutorial.
thanks for your input!
Thomas