Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Open
gekoramy opened this issue Jun 28, 2021 · 5 comments
Open

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

gekoramy opened this issue Jun 28, 2021 · 5 comments

Comments

@gekoramy
Copy link

gekoramy commented Jun 28, 2021

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
Copy link
Member

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 gekoramy changed the title Add Predicate[N]<Tuple[N]<T...>> Tuple.predicate(Predicate[N]<T...>) Add Predicate<Tuple[N]<T...>> Tuple.predicate(Predicate[N]<T...>) Jun 28, 2021
@gekoramy
Copy link
Author

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
Copy link
Member

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
Copy link
Author

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
Copy link
Member

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

magicprinc added a commit to magicprinc/jOOL that referenced this issue Oct 18, 2022
…<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)));
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants