https://kotlinlang.org logo
#arrow
Title
# arrow
p

Petr Laštovička

01/26/2023, 12:45 PM
Hi, I have an usecase, where I want to convert a list of entities to another entities. It is possible that this conversion results in an exception. If so I want to record the error, ignore the item and continue mapping. After the operation, I want to have a List of errors and a List of converted entities. I can think of using fold with two persistent list accumulators. Do you see a better way of doing something like this? Thanks
s

simon.vergauwen

01/26/2023, 12:53 PM
Hey @Petr Laštovička, You can use
Either
for this with
seperateEither
. https://arrow-kt.io/docs/apidocs/arrow-core/arrow.core/separate-either.html Something like this:
Copy code
fun convert(): Either<Throwable, Entity> =
  Either.catch { ... }

val (errors, entities) = list.map { convert() }
    .seperateEither()
p

Petr Laštovička

01/26/2023, 12:56 PM
Very cool, thanks arrow
s

simon.vergauwen

01/26/2023, 1:10 PM
My pleasure ☺️
6 Views