Reputation: 5464
Has anybody written a library in java that provides mapping functions (such as mapcar from lisp).
I saw this post and few others (such as this one this one), but sadly nothing that I could consider mainstream and/or usable.
Upvotes: 1
Views: 659
Reputation: 223023
You'll be looking forward to Java 8, then! It will have Project Lambda included, which has a much, much nicer syntax for closure-like anonymous classes.† Example:
Iterable<String> strs = ...
Iterable<String> downCased = strs.map(s -> s.toLowerCase());
Any interface with one method (or abstract class with one abstract method) can use this syntax, including Guava's Function
and Predicate
(though Java 8 has its own Mapper
and Predicate
interfaces, so these are usable out of the box). In this case Iterable.map
is a new extension method that takes a new interface type called Mapper
.
If you'd like more examples of Java 8 lambdas, just ask!
† All the usual restrictions of anonymous classes still apply, including that local free variables must be "effectively final". This means you don't have to explicitly tag the variable as final
, but you're still not allowed to alter the value.
Upvotes: 2
Reputation: 47183
There are a few. They are usually described as something like "functional programming in Java" libraries rather than by reference to LISP.
At my company, the functional programming druids settled on Functional Java as their preferred library, although there is a significant and vocal minority who prefer the functional-esque provisions in Guava.
Guava is a very mainstream and popular library; it's firmly in the "nobody got fired for using" category. FJ may be less well known, but we're using it pretty happily. We've even forked it so we can help improve it.
Upvotes: 6