java-8functional-programmingvavr

Combining multiple `Try` instances of different result types as applicatives


Is there an api for combining Try instances in Vavr that is similar to the way the Scalaz applicative operator |@| works?

Specifically, if I have more than two Try instances, for example, Try<X>, Try<Y> and Try<Z>, I would like to combine these instances in an applicative fashion using a 3-arg function.

I'm looking for a function signature that is similar to:

static <X, Y, Z, R> Try<R> combine(Try<X> x, Try<Y> y, Try<Z> z, Function3<X,Y,Z,R> func

Solution

  • As far as I can see it doesn't support that usage directly. You can, however, achieve it using flatMap:

    static <X, Y, Z, R> Try<R> combine(Try<X> tx, Try<Y> ty, Try<Z> tz, Function3<X,Y,Z,R> func) {
        return tx.flatMap(x -> ty.flatMap(y -> tz.map(z -> func.apply(x, y, z))));
    }
    

    If each Try value contains the same type then you can use a sequence operation:

    public static void main(String[] args) {
        List<Try<String>> lt = List.of(Try.success("A"), Try.success("B"), Try.success("C"));
        Try<List<String>> tl = sequence(lt);
        System.out.println(tl);
    }
    
    static <T> Try<List<T>> sequence(List<Try<T>> lt) {
        return lt.foldRight(
            Try.success(List.empty()),
            (tt, tl) -> tt.flatMap(t -> tl.flatMap(l -> Try.success(l.prepend(t))))
        );
    }
    

    If you compare the input and output types you can see this essentially swaps the position of the Try and List containers. It's fairly idiomatic for monads, though typically you would implement it with applicative map operations instead of flatMap.

    Alternatively, use Validation, which is designed to be used in an applicative style (via Validation.combine).