jOOQ / jOOL

jOOλ - The Missing Parts in Java 8 jOOλ improves the JDK libraries in areas where the Expert Group's focus was elsewhere. It adds tuple support, function support, and a lot of additional functionality around sequential Streams. The JDK 8's main efforts (default methods, lambdas, and the Stream API) were focused around maintaining backwards compatibility and implementing a functional API for parallelism.
http://www.jooq.org/products
Apache License 2.0
2.09k stars 168 forks source link

Add Seq.collect(Collector, Collector, ..., Collector) for convenience #345

Closed lukaseder closed 5 years ago

lukaseder commented 5 years ago

Type inference tends not to work very well when using Tuple.collectors(). A type witness is often needed as can be seen in this post: https://blog.jooq.org/2019/02/11/lesser-known-joo%CE%BB-features-useful-collectors

// Unfortunately, Java's type inference might need
// a little help here
var percentiles =
Stream.of(1, 2, 3, 4, 10, 9, 3, 3).collect(
  Tuple.collectors(
    Agg.<Integer>percentile(0.0),
    Agg.<Integer>percentile(0.25),
    Agg.<Integer>percentile(0.5),
    Agg.<Integer>percentile(0.75),
    Agg.<Integer>percentile(1.0)
  )
);

System.out.println(percentiles);

Wrapping the collectors explicitly shouldn't be necessary, really. It would be nice to have overloaded collect() methods that allow for collecting into a tuple directly:

// Unfortunately, Java's type inference might need
// a little help here
var percentiles =
Stream.of(1, 2, 3, 4, 10, 9, 3, 3).collect(
    Agg.percentile(0.0),
    Agg.percentile(0.25),
    Agg.percentile(0.5),
    Agg.percentile(0.75),
    Agg.percentile(1.0)
);

System.out.println(percentiles);
lukaseder commented 5 years ago

This feature is already implemented :-)