I just wonder what's the best way to apply a function that returns Void
on an Iterable/Collection
?
My usecase is:
Animal
objectseat()
functionI 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:
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)
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);
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With