-
Notifications
You must be signed in to change notification settings - Fork 168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Consumer1<Tuple[N]<T1, ..., TN>> Tuple.consumer(Consumer[N]<T1, ..., TN>) and Function1<Tuple[N]<T1, ..., TN>> Tuple.function(Function[N]<T1, ..., TN>) #214
Comments
Something like the following allows it to other public class TupleLambdas {
public static <A, B> Predicate<Tuple2<A, B>> predicate2(Function2<A, B, Boolean> predicate) {
return (tuple) -> predicate.apply(tuple.v1, tuple.v2);
}
public static <A, B, R> Function<Tuple2<A, B>, R> function2(Function2<A, B, R> function) {
return (tuple) -> function.apply(tuple.v1, tuple.v2);
}
public static <A, B, C> Predicate<Tuple3<A, B, C>> predicate3(Function3<A, B, C, Boolean> predicate) {
return (tuple) -> predicate.apply(tuple.v1, tuple.v2, tuple.v3);
}
public static <A, B, C, R> Function<Tuple3<A, B, C>, R> function3(Function3<A, B, C, R> function) {
return (tuple) -> function.apply(tuple.v1, tuple.v2, tuple.v3);
}
...
} |
Thank you very much for your suggestion. There are some good ideas here, and there's definitely appetite for it. There's no need explicitly add the degree to Note, this could go both ways. There is also potential of adding, e.g. interface Tuple2<T1, T2> {
static BiPredicate<T1, T2> biPredicate(Predicate<? super Tuple2<T1, T2>> predicate);
static <R> BiFunction<T1, T2, R> biFunction(Function<? super Tuple2<T1, T2>, ? extends R> function);
} The question is, where to put these methods... |
I wasn't sure if it would be necessary to add the degree to the methods, I thought probably not, but erred on the side of caution (as Java always likes to pretend that it's worse at implicit resolutions than it actually is). The issue with consumers was that there is no consumer above 2nd degree, I tried to get around this by using a So, you'd have something like: public static <A, B, C> Consumer<Tuple3<A, B, C>> consumer(Function3<A, B, C, Void> function) {
return (tuple) -> function.apply(tuple.v1, tuple.v2, tuple.v3);
} But then to use it would need to do something like: Seq.of(tuple(1, 'a', "hello"), tuple(1, 'a', "hello")).forEach(consumer((i, c, s) -> {
System.out.println(i + " - " + c + " - " + s);
return null;
})); If there was a I may be missing a solution to this problem though, or something else in the toolbox that could be used. |
|
…]<T1, ..., TN>) and Function1<Tuple[N]<T1, ..., TN>> Tuple.function(Function[N]<T1, ..., TN>)
I've added your original suggestion for My own suggestion will be treated in a separate issue |
Thanks again for your suggestion. I think this will be very useful! If you have any other ideas, just shoot, always happy to discuss |
Without I use |
Huh, that would be the missing use-case in #215 (comment)... There's no problem with adding the However, I don't want to add I'll review this |
From your comments in #215 I had it in my mind that you would be implementing |
Yes, that might seem reasonable at first, but it might be unwise to ignore the status quo in the JDK APIs and not support <T1, T2> Predicate<Tuple2<T1, T2>> predicate(BiPredicate<? super T1, ? super T2> predicate);
<T1, T2> Predicate<Tuple2<T1, T2>> predicate(Function2<? super T1, ? super T2, Boolean> predicate); While the overload is legal, it makes using lambdas impossible, as there would be ambiguity (unless the lambda is explicitly cast, which is nasty). Tough choice... |
I think the case is probably building for implementing |
Another thought on this is some way to make it play nicely with Personally, I'd be happy for all these methods to automatically wrap in unchecked versions, because I think checked exceptions are Devil spawn, but realise others may disagree. |
Indeed, that might be useful at some point. I've created #225 for this. Let's see if it gets any traction first. I feel that this starts exploding exponentially in the number of types that need to be supported... |
Does anything like the following currently exist in jOOL, or would there be any appetite for adding something like it? I created my own, as I couldn't find anything.
This allows the following:
or:
rather than the more long winded:
or more opaque:
Most times when I've rolled my own solutions to Java's lambda limitations I then find jOOL has beaten me to it, but I couldn't find anything in this case.
The text was updated successfully, but these errors were encountered: