Skip to content

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

Closed
@lukens

Description

@lukens

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions