jOOL icon indicating copy to clipboard operation
jOOL copied to clipboard

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

Open gekoramy opened this issue 4 years ago • 5 comments

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)));

gekoramy avatar Jun 28 '21 10:06 gekoramy

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.

lukaseder avatar Jun 28 '21 15:06 lukaseder

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>)

gekoramy avatar Jun 28 '21 16:06 gekoramy

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.

lukaseder avatar Jun 28 '21 17:06 lukaseder

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

gekoramy avatar Jun 28 '21 20:06 gekoramy

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

lukaseder avatar Jun 28 '21 21:06 lukaseder