Is there a way to call Validated.zip on an Iterabl...
# arrow
j
Is there a way to call Validated.zip on an Iterable instead of one of the overloaded forms? Is this a JVM optimization (I’ve seen Clojure do similar overloading instead of vararg). eg.
Copy code
val someIterable : List<Validated<MyError, String>> // from a map or something similar
someIterable.zip(Semigroup.nonEmptyList<MyError>()} {array -> ... }
//or
Validated.zip(someIterable, se) {array -> ... }
I’m borrowing Rx’s api for zips / FuncN for f. Is this a JVM optimization? (I’ve seen Clojure do similar overloading instead of a single vararg form)
s
Hey @Jeff, If you’re looking for
zip
on
Iterable
I think you’re looking for
traverseXXX
. In case of validated
traverseValidated
. There is also
parTraverseValidated
inside Arrow Fx Coroutines in case you want to run validation code in parallel.
The signature however looks like this:
Copy code
val someIterable: List<String> = ...

fun validate(str: String): ValidatedNel<MyError, String>

someIterable.traverseValidated { str ->
  validate(str)
} // ValidatedNel<MyError, String>
The reason we don’t use
vararg
here is because there is a different type in every param. So you can combine
A
,
B
,
C
into
D
. Where with
vararg
all params have to be of the same type, so you’re restricted to
A
,
A
,
A
into
D
.
So this is also the difference between
zip
and
traverse
. With
zip
you can combine values that of different types, while
traverse
works on a container (Iterable) which holds values of the same type.
j
makes sense. thanks!