Seing a Collector as a Gatherer

Remi Forax forax at univ-mlv.fr
Wed Jan 17 16:08:00 UTC 2024


Hello,
I may have overlook that, but it seems there is no method to see a Collector as a Gatherer.

A Gatherer is more general than a Collector and a Gatherer with a greedy integrator that does not call Downstream.push in the intergator and only once is the finisher is basicaly a Collector.

In code:
<E, A, T> Gatherer<E, A, T> asGatherer(Collector<? super E, A, ? extends T> collector) {
  var supplier = collector.supplier();
  var accumulator = collector.accumulator();
  var combiner = collector.combiner();
  var finisher = collector.finisher();
  return Gatherer.of(supplier,
      Gatherer.Integrator.ofGreedy((state, element, _) -> {
        accumulator.accept(state, element);
        return true;
      }),
      combiner,
      (state, downstream) -> downstream.push(finisher.apply(state)));
}

This is eaxctly how Gatherer.fold() works.

Is there a reason why such method does not exist ?

regards,
Rémi


More information about the core-libs-dev mailing list