weixin_39789094 2020-11-29 20:12 采纳率: 0%
浏览 0

Add Consumer1<Tuple[N]<T1, ..., TN>> Tuple.consumer(Consumer[N]<T1, ..., TN>) and Function1<Tuple[N]<T1, ..., TN>> Tuple.function(Function[N]<T1, ..., TN>)

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.

 java
public class Tuple2Lambdas {

    public static <a b> Predicate<tuple2 b>> predicate(BiPredicate<a b> predicate) {

        return (tuple) -> predicate.test(tuple.v1, tuple.v2);

    }

    public static </a><a b r> Function<tuple2 b>, R> function(BiFunction<a b r> function) {

        return (tuple) -> function.apply(tuple.v1, tuple.v2);

    }

    public static </a><a b> Consumer<tuple2 b>> consumer(BiConsumer<a b> consumer) {

        return (tuple) -> consumer.accept(tuple.v1, tuple.v2);

    }

}
</a></tuple2></a></tuple2></a></tuple2></a>

This allows the following:

 java
        Seq.seq(tuple2List)
            .filter(predicate(this::validHit))
            .map(function(this::getDisplayMatch))
            .forEach(consumer(this::processResult));

or:

 java
        Seq.seq(tuple2List)
            .filter(predicate((hit, property) -> hit.getId().equals("blah")))
            .map(function((hit, property) -> tuple(hit.explanation(), property.getId())))

rather than the more long winded:

 java
        Seq.seq(tuple2List)
            .filter(tuple -> validHit(tuple.v1, tuple.v2))
            .map(tuple -> getDisplayMatch(tuple.v1, tuple.v2))
            .forEach(tuple -> processResult(tuple.v1, tuple.v2));

or more opaque:

 java
        Seq.seq(tuple2List)
            .filter((tuple) -> tuple.v2.getId().equals("blah"))
            .map((tuple) -> tuple(tuple.v1.explanation(), tuple.v2.getId())

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.

该提问来源于开源项目:jOOQ/jOOL

  • 写回答

1条回答 默认 最新

  • weixin_39789094 2020-11-29 20:12
    关注

    Something like the following allows it to other TupleNs (though consumers are trickier, and would probably require ConsumerNs to match the FunctionNs [and the predicates are a little hacky]):

     java
    public class TupleLambdas {
    
        public static <a b> Predicate<tuple2 b>> predicate2(Function2<a b boolean> predicate) {
    
            return (tuple) -> predicate.apply(tuple.v1, tuple.v2);
    
        }
    
        public static </a><a b r> Function<tuple2 b>, R> function2(Function2<a b r> function) {
    
            return (tuple) -> function.apply(tuple.v1, tuple.v2);
    
        }
    
        public static </a><a b c> Predicate<tuple3 b c>> predicate3(Function3<a b c boolean> predicate) {
    
            return (tuple) -> predicate.apply(tuple.v1, tuple.v2, tuple.v3);
    
        }
    
        public static </a><a b c r> Function<tuple3 b c>, R> function3(Function3<a b c r> function) {
    
            return (tuple) -> function.apply(tuple.v1, tuple.v2, tuple.v3);
    
        }
    
    ...
    
    }
    </a></tuple3></a></tuple3></a></tuple2></a></tuple2></a>
    评论

报告相同问题?