I receive an Either<E, List<A>>
from a function call and need to transform the List<A>
into a List<B>
. The transformation of each A returns an Either<E,B>
, so that my result is an Either<E, List<Either<E,B>>>
. How can I turn the Either<E, List<Either<E,B>>>
into an Either<E, List<B>>
,
- if all transformation succeeded (Should result in
Either.Left
, if a single transformation fails) - create an
Either<E,List<B>>
containing all Bs, for which the transformation succeeded and ignoring failed AtoB transformations
A little code snippet below:
fun getListOfA(): Either<Exception, List<A>> {
TODO()
}
fun A.transformAtoB(): Either<Exception, B> {
TODO()
}
fun getListB(): Either<Exception, List<B>> {
return getListOfA().map {
// this is now Either<Exception, List<Either<Exception, B>>>
listOfA -> listOfA.map { it.transformAtoB() }
// ????? => Either<E, List<B>>
}
}