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
As far as I can see it doesn't support that usage directly. You can, however, achieve it using flatMap:
If each Try value contains the same type then you can use a
sequence
operation:If you compare the input and output types you can see this essentially swaps the position of the
Try
andList
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
).