What’s the best way to transform a `List<Either...
# arrow
j
What’s the best way to transform a
List<Either<Error, Id>>
to a
Either<Error, List<Id>>
? I want a
Either.Left
return as soon as possible, not a list of all result eventually containing errors. Either an error immediately when something fails or a list of valid result
s
Hey @jean, You can use
sequenceEither()
to turn
List<Either<Error, Id>>
into
Either<Error, List,<Id>>
whilst returning as soon as a first
Either.Left
is found. Alternatively, you can also use
traverseEither
to apply
(A) -> Either<E, B>
to
List<A>
and get back
Either<E, List<B>>
.
This function also exists for
Option
,
Validated
, and more. And there are also parallel version of this called
parTraverseXXX
&
parSeauenceXXX
inside Arrow Fx Coroutines
🤩 1
j
are those methods located in arrow.core? IntelliJ can resolve Either but not the functions you listed in this thread
s
Yes, they’re available in
arrow.core.*
j
oh I get it now, it should be applied as an operator, not something like
sequenceEither { … }
thanks, it solved my issue 🙂
👍 1
👍🏼 1