Given a value foo
and a Stream
of Consumer<Foo>
void functions, what's the most concise way to apply each function to the value? Right now I have
consumers.forEach(c -> c.accept(foo));
which isn't terrible, but I suspect there might be some way to turn this inside out and do it with just method references (no explicit lambda). Possibly something with a singleton list and zip()
?
I'd be happy with a straight Java 8 answer, or Vavr, or Scala.
(Note that this is not a fold, or at least not the usual foldLeft
/foldRight
application; if there were return values, I'd be discarding them, not iterating on them.)
You could reduce your consumers by means of the
Consumer.andThen
method and then apply the resulting consumer to thefoo
argument:But there's no difference between this approach and yours, and yours' is shorter.