5

I read an article on Joel On Software about the idea of using higher order functions to greatly simplify code through the use of map and reduce. He mentioned that this was difficult to do in Java. The article: http://www.joelonsoftware.com/items/2006/08/01.html

The example from the article below, loops through an array, and uses the function fn that was passed as an argument on each element in the array:

function map(fn, a)
{
    for (i = 0; i < a.length; i++)
    {
        a[i] = fn(a[i]);
    }
}

This would be invoked similar to the below in practice:

map( function(x){return x*2;}, a );
map( alert, a );

Ideally I'd like to write a map function to work on arrays, or Collections of any type if possible.

I have been looking around on the Internet, and I am having a difficult time finding resources on the subject. Firstly, are anonymous functions possible in java? Is this possible to do in another way? Will it be available in a future version of java? If possible, how can I do it?

I imagine that if this is not possible in Java there is some kind of 'pattern'/technique that people use to achieve the same effect, as I imagine anonymous functions are a very powerful tool in the software world. the only similar question I was able to find was this: Java generics - implementing higher order functions like map and it makes absolutely no sense to me.

Community
  • 1
  • 1

5 Answers5

5

Guava provides map (but it's called transform instead, and is in utility classes like Lists and Collections2). It doesn't provide fold/reduce, however.

In any case, the syntax for using transform feels really clunky compared to using map in Scheme. It's a bit like trying to write with your left hand, if you're right-handed. But, this is Java; what do you expect. :-P

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
2

Looks like this one?

How can I write an anonymous function in Java?

P.S: try Functional Java. Maybe it could give you hints.

Community
  • 1
  • 1
anta40
  • 6,511
  • 7
  • 46
  • 73
2

Single method anonymous classes provide a similar, but much more verbose, way of writing an anonymous function in Java. For example, you could have:

Iterable<Source> foos = ...;
Iterable<Destination> mappedFoos = foos.map(new Function<Source, Destination>() 
{
    public Destination apply(Source item) { return ... }
});

For an example of a Java library with a functional style, see Guava

Matt H
  • 7,311
  • 5
  • 45
  • 54
  • 1
    Indeed it seems like if/when Java eventually gets lambda expressions, they will be convertible to such an anonymous class. – hammar Apr 26 '11 at 07:25
1
interface Func<V,A> {
    V call (A a);
}

static <V,A> List<V> map (Func<V,A> func, List<A> as) {
    List<V> vs = new ArrayList<V>(as.size());
    for (A a : as) {
        Vs.add(func.call(a));
    }
    return vs;
}
Tom
  • 43,583
  • 4
  • 41
  • 61
0

Paguro has an open-source implementation of higher order functions. Initial test show it to be 98% as fast as the native Java forEach loop. The operations it supports are applied lazily without modifying the underlying collection. It outputs to type-safe versions of the immutable (and sometimes mutable) Clojure collections. Transformable is built into Paguro's unmodifiable and immutable collections and interfaces. To use a raw java.util collection as input, just wrap it with the xform() function.

GlenPeterson
  • 4,866
  • 5
  • 41
  • 49