Is there an alternative to `mapOrAccumulate` and `...
# arrow
j
Is there an alternative to
mapOrAccumulate
and
parMapOrAccumulate
that fails at the first “left” rather than accumulating it? I’m trying to map a list of object to another type and wish to either get a “left” error at the first fail or get the whole new list.
I’m doing this for now
Copy code
objects.mapOrAccumulate { it.toOtherType().bind() }
    .mapLeft { it.first() }
It’s fine enough but it might be an function in the library doing that already?
e
couldn't you just do 🤔
Copy code
either { objects.map { it.toOtherType().bind() } }?
same 1
a
for a more general reply, every "normal" collection function (
map
.,
foreach
, ...) already works in a left-biased way, so you don't need special operators for them. You only need to use
OrAccumulate
versions when you want processing to continue after the first error
j
If I use a normal
map
I end up with a
List<Either<Error, OtherType>>
rather than a
Either<Error, List<OtherType>>
. Wasn’t there a
traverse()
method to convert a list of either to a either of list actually?
a
the trick is to call the
map
inside an
either
context, as mentioned by @Emil Kantis above
j
Yes, I just find it a little less elegant, but it works. Thanks for the tips!
s
Quiver will support this kind-of fluent operators if you still prefer to use
traverse
in the future. I don't think they'll go as far as to also support
parTraverse
, etc. This also points to the reason for the DSL, it doesn't require any of these APIs and thus the regular APIs "just work". Additionally, after working with it a lot we felt it consistently resulted in less code than before.