Closed
Description
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.
public class Tuple2Lambdas {
public static <A, B> Predicate<Tuple2<A, B>> predicate(BiPredicate<A, B> predicate) {
return (tuple) -> predicate.test(tuple.v1, tuple.v2);
}
public static <A, B, R> Function<Tuple2<A, B>, R> function(BiFunction<A, B, R> function) {
return (tuple) -> function.apply(tuple.v1, tuple.v2);
}
public static <A, B> Consumer<Tuple2<A, B>> consumer(BiConsumer<A, B> consumer) {
return (tuple) -> consumer.accept(tuple.v1, tuple.v2);
}
}
This allows the following:
Seq.seq(tuple2List)
.filter(predicate(this::validHit))
.map(function(this::getDisplayMatch))
.forEach(consumer(this::processResult));
or:
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:
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:
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.