13

I just wonder what's the best way to apply a function that returns Void on an Iterable/Collection?

My usecase is:

  • i have a list of Animal objects
  • i want to call on each animal of the list the eat() function

I have a Function<Animal,Void> which calls input.eat();

It turns out that when i call:

Collections2.transform(animalList,eatFunction);

I dont find this very elegant since i'm not looking for a transformation, but only for an application without any output. Worst, it does not even work since the Guava transformations are returning views.

What works fine is:

Lists.newArrayList( Collections2.transform(animalList,eatFunction) );

But it's not elegant. What is the best way to apply a Void function to an Iterable/Collection, in a functional way with Guava?

Edit:

Community
  • 1
  • 1
Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419
  • 7
    Just do a traditional for-each loop and call `animal.eat()`? If you don't need laziness and don't transform, don't use Guava's tranfsorm at all. – Grzegorz Rożniecki Sep 14 '12 at 15:01
  • I'd like to not use transform, it's what i'm asking for. And it's still more concice and readable to use Guava functional style to apply the call to each object – Sebastien Lorber Sep 14 '12 at 15:06
  • What can I say - it's subjective that applying is more readable in this case. [And Guava team members think it's not](http://stackoverflow.com/questions/9525951/anything-in-guava-similar-to-functional-javas-effect/9526157#9526157). – Grzegorz Rożniecki Sep 14 '12 at 15:15
  • 3
    @Sebastien Lorber: Your `eat` method returns nothing, so from a functional point of view it's pointless to call it. What you're trying to do is to apply functional style to a procedural thing, and that's not supported. Without closures the functional style is Java is too verbose anyway and no wonder that it's support is limited to where it fits best. – maaartinus Sep 14 '12 at 17:43

3 Answers3

28

What do you think is more elegant? A plain old for loop:

for (Animal animal : animalList)
    animal.eat();

or a "bending a procedural language by writing a procedural operation in a functional style" madness?

static final Function<Animal, Void> eatFunction = new Function<Animal, Void>() {
    @Override
    public Void apply(Animal animal) {
        animal.eat();
        return null; // ugly as hell, but necessary to compile
    }
}

Lists.newArrayList(Collections2.transform(animalList, eatFunction));

I would vote for the first case.

If you really would like to write your programs in functional style, I would recommend switching to another JVM language.

Scala might be a good alternative for such a case:

animalList.foreach(animal => animal.eat)

or even a shorter variant using the _ placeholder:

animalList.foreach(_.eat)

EDIT:

After trying the code in Eclipse I found out that I had to add the return null statement to the eatFunction, because 1) Void isn't the same as void and 2) it is uninstantiable. That's even more ugly then expected! :)

Also from performance point of view, the invocation of the lazy function by using some copy constructor like above also pointlessly allocates memory. An ArrayList of the same size as the animalList filled only with nulls is created just to be immediately garbage collected.

If you really have a use case where you want to pass around some function objects and dynamically apply them on some collections, I would write my own functional interface and a foreach method:

public interface Block<T> {
    void apply(T input);
}

public class FunctionUtils {
    public static <T> void forEach(Iterable<? extends T> iterable,
            Block<? super T> block) {
        for (T element : iterable) {
            block.apply(element);
        }
    }

}

Then you can similarly define a void (lower case) function:

static final Block<Animal> eatFunction = new Block<Animal>() {
    @Override
    public void apply(Animal animal) {
        animal.eat();
    }
};

And use it like this:

FunctionUtils.forEach(animalList, eatFunction);
// or with a static import:
forEach(animalList, eatFunction);
hichris123
  • 10,145
  • 15
  • 56
  • 70
Natix
  • 14,017
  • 7
  • 54
  • 69
  • 4
    Sure, the hacked declarations of the function-like classes are ugly, but in cases which require applying a parameterized action or a series of actions, how does one do that without an implementation of the Visitor pattern and a functor-like object? So there are in fact reasonable use cases for this. The same argument is made against using verbose true function declarations in functional APIs, even by the Guava team themselves, but they implemented Function anyway because they realized there were use cases which called for it, as long as the disadvantages were carefully weighed first. – Kevin Welker Sep 14 '12 at 20:35
  • A usecase for such a need is when you transform one Optional to another and then want to apply a function on the last object of the chain only if it's present – Sebastien Lorber Sep 15 '12 at 18:06
  • @Natix: You could optimize the `ArrayList` away by replacing `Lists.newArrayList` by a `DummyUtils.newEmptyStayingCollection` implementing `add` as a no-op (which IMHO doesn't violate the `Collection` contract). – maaartinus Sep 16 '12 at 07:46
  • @maaartinus That's just a horrible hack. :) – Natix Sep 16 '12 at 11:18
  • @Natix: No that's a zero-capacity non-blocking queue (or set). :D:D – maaartinus Sep 16 '12 at 18:09
  • Classic for loop of course works fine, just don't forget about the brackets because it's always a good practice to use them. – Rafał Oct 14 '14 at 14:00
  • This answer is helpful but it's a false dilemma to make everyone who wants to use Functional constructs switch to scala. The people who want to use functional style don't get to pick the language they code in. – Sridhar Sarnobat May 20 '16 at 18:38
7

I was just looking for the same thing and found a Java Consumer interface. In your case it would be:

final Consumer<Animal> action = new Consumer<Animal>() {
    @Override
    public void accept(Animal animal) {
        animal.eat();
    }
};    
...
FluentIterable.from(animals).forEach(action);
Dzik
  • 399
  • 1
  • 5
  • 17
5

As others pointed out, Guava team has a viewpoint that precludes doing this. If you are looking for other functor-like APIs for doing what you want, you could check out Functional Java's Effect, or Jedi's Command class, or Play! framework's F.Callback, or Commons Collections4's Closure [later edit:] or Java 8+ Consumer-based interfaces.

Kevin Welker
  • 7,719
  • 1
  • 40
  • 56