What's the best way to "flatMap" two or more valid...
# arrow
s
What's the best way to "flatMap" two or more validated nels ?
Copy code
sequenceValidated
?
I kinda wanna do something like this
Copy code
val p1: (T) -> ValidatedNel<E, A>
val p2: (T) -> ValidatedNel<E, B>
p1(t).flatMap { a -> p2(t).map { b-> Pair(a, b) } }
w
Is
zip
what you're looking for?
Copy code
p1(t).zip(p2(t)) { a, b -> Pair(a, b) }
I think that works, not sure though.
s
Yep that's it, thanks @Wesley Hartford
w
Awesome, glad to help
r
In addition to zip as @Wesley Hartford pointed out one can bind Validated values inside
either
blocks and use them as if they were of type
Either
.
Copy code
either<E> {
  Pair(p1.bind(), p2.bind())
}
The difference is that if you bind, it would short-circuit instead of accumulating errors when any of the values is of type
Invalid
👍🏻 1