Hei. I was wondering if I can achieve something si...
# arrow
s
Hei. I was wondering if I can achieve something similar with
IO
like we have in
Validated
by using Semigroup with applicative, we can get a list of all the failures. I see we have
IOSemiGroup
which has combine method, but I don’t it will give me the desired effect right ? Because
IO
encapsulates
Throwable
, so it will be always
IO<A>
while
Throwable
will be first failure while combining. I have seen the same thing with
parTraverse
on
IO
. I got the desired effect by giving
myIOOp.attempt()
to
parTraverse
. Then I get
IO<List<Either<Throwable, A>>>
, so with this I can extract out all failures. But it becomes complicated quickly.
Validated applicative
is neat, and uses
tupledN
which is nice so I don’t have to resort to coverting to
List
first. So, is there a way ?
s
Using Arrow Fx Coroutines this can become quite simple. There is
parTraverse
for
Iterable<A>
.
val List<ValidatedNel<Throwable, A>> = listOf(1, 2, 3).parTraverse { i -> Validated.catch { f(i) } }
And then you can
sequence
List<ValidatedNel<Throwable, A>>
to
ValidatedNel<Throwable, List<A>>
. Looking at the implementation of
parTraverse
and the impl of
List.sequence
you can probably flatten these 2 operations into a single one.
With
IO
you can follow the same pattern, but it'll involve a 3rd wrapper
IO
instead of
suspend
.
s
Right. Thanks 🙂