jean
07/12/2023, 12:34 PMList<Either<CutomErrorType, Unit>>
and I want to transform it into a Either<List<CustomErrorType>, Unit>
, how can I do that? I know traverse
will give me a Either<CustomErrorType, List<Unit>>
but I need a sort of traverseLeft
that doesn’t seem to exist.Youssef Shoaib [MOD]
07/12/2023, 12:47 PM.map(Either::swap).traverse()
simon.vergauwen
07/12/2023, 3:37 PMmapOrAccumulate { it.bind() }
That should work, right?simon.vergauwen
07/12/2023, 3:37 PMEither<NonEmptyList<CustomErrorType>, List<Unit>>
but you can swallow List<Unit>
into Unit
Youssef Shoaib [MOD]
07/12/2023, 4:22 PMList<Either<CustomErrorType, Unit>>
is even needed, or if your code could be restructured into running all those error-producing functions inside of mapOrAccumulate
and perhaps even using a Raise receiver instead of returning that Eitherjean
07/13/2023, 8:21 AMmapOrAccumulate
available in 1.2.0 and above? I haven’t migrated yet, I guess it’s time hehe.simon.vergauwen
07/13/2023, 8:21 AMjean
07/13/2023, 8:49 AMmapOrAccumulate
transforms my Either<CustomErrorType, List<Unit>>
into a Either<NonEmptyList<CustomErrorType>, List<List<Unit>>>
so now I need to figure out how to flatten the “right” result.jean
07/13/2023, 8:57 AMEither<Error, List<UserProfile>>
. At first I was saving all the freshly fetched lists of profiles in database, thus returning Either<List<CustomErrorType>, Unit>
with Unit
if all the insertions went fine. But I took it out to simplify that part of the code a bit.simon.vergauwen
07/13/2023, 9:05 AMsimon.vergauwen
07/13/2023, 9:08 AMparMapOrAccumulate
which combines parallel mapping and mapOrAccumulate
in a single operation whilst hiding all the CoroutineScope
, async/await
mechanics. This also fixes the bug where you have to use .awaitAll
instead of map { it.await() }
. It also makes fetchUserProfileAsync
a bit simpler since it's no longer aware it runs Async
. It's just a simple suspend
definition, and the caller decides how it runs.
To flatten, the easiest thing is just to use map { it.flatten() }
, perhaps we should investigate flatMapOrAccumulate
for future versions. I've seen this map { it.flatten() }
come up a couple times now.simon.vergauwen
07/13/2023, 9:08 AMRaise
but let me share a separate snippet.simon.vergauwen
07/13/2023, 9:09 AMsimon.vergauwen
07/13/2023, 9:11 AMEither
is replaced by an extension on Raise<Error>
and bind()
disappears. There is no longer a need to wrap in right
and when you wrap in left
you instead call raise(x)
.simon.vergauwen
07/13/2023, 9:11 AMjean
07/13/2023, 9:12 AMRaise
type yet, but it look quite similar to Either with less boiler plate. Thanks for the help @simon.vergauwen!simon.vergauwen
07/13/2023, 9:27 AMRaise
will become more powerful with context receivers, since you can do some things that Either
cannot do like compose two errors. context(Raise<Error1>, Raise<Error2>)
.