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