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.08k stars 167 forks source link

Add Predicate<Tuple[N]<T...>> Tuple.predicate(Predicate[N]<T...>) #391

Open gekoramy opened 3 years ago

gekoramy commented 3 years ago

I re-propose attention to an old issue that has not yet been fully solved: #214

Currently jOOL offers:

Function<Tuple[N]<T...>, R> Tuple.function(Function[N]<T..., R>)
Consumer<Tuple[N]<T...>>    Tuple.consumer(Consumer[N]<T...>)

But it misses:

Predicate<Tuple[N]<T...>>   Tuple.predicate(Predicate[N]<T...>)

In order to beautify stream processing like that:

Seq.of(tuple("marco", 24), tuple("luigi", 16), tuple("maria", 18))
   .filter(Tuple.predicate((name, age) -> age > 17))
   .map(Tuple.function((name, age) -> tuple("Sir / Madame " + name, age)))
   .forEach(Tuple.consumer((name, age) -> process(options, name, age)));
lukaseder commented 3 years ago

Thanks a lot for your suggestion. I can see how this could help in your example, though it's not strictly necessary. You can always just filter(t -> t.v2 > 17).

These Function[N] and Consumer[N] types are the minimum required functional interfaces of degree N to model both value-compatible and void-compatible lambdas. A Predicate<T> is just a Function<T, Boolean>, and as such, wasn't added. The same is true for the equivalents of ToIntFunction, ToLongFunction, ToDoubleFunction, and all the rest.

I would need a few more applications than this convenience to justify adding another 16 interfaces.

gekoramy commented 3 years ago

I see your point

What about an implementation that does not require any Predicate[N] at all?


Predicate<Tuple0>         Tuple.predicate(Function0<Boolean>)

Predicate<Tuple1<T1>>     Tuple.predicate(Function1<T1, Boolean>)

Predicate<Tuple2<T1, T2>> Tuple.predicate(Function2<T1, T2, Boolean>)

...

Predicate<Tuple[N]<T...>> Tuple.predicate(Function[N]<T..., Boolean>)
lukaseder commented 3 years ago

I had thought of it. A Predicate offers 2-valued booleans, a Function<?, Boolean> technically supports 3-valued booleans (null), so I'm not too happy with that workaround.

gekoramy commented 3 years ago

You are right I didn't think about the null value After all, it must be for this reason that Java devs have introduced the Predicate<T> interface

lukaseder commented 3 years ago

Well, we might get Function<?, boolean> some time soon, in case of which that might indeed work.