raulraja
06/03/2019, 11:29 AMraulraja
09/01/2022, 9:59 PMFoldable
or those types but I think what you are trying to achieve can be defined as:
fun <E, A> Iterable<Either<E, A>>.partitionEither(): Pair<List<Left<E>>, List<Right<A>>> =
fold(emptyList<Left<E>>() to emptyList<Right<A>>()) { (lefts, rights), either ->
when (either) {
is Left -> lefts + either to rights
is Right -> lefts to rights + either
}
}
raulraja
09/01/2022, 10:01 PMmitch
09/01/2022, 11:03 PMpublic fun <T> Iterable<Either<E, T>>.partitionEither(): Pair<List<T>, List<E>> {
val values = mutableListOf<T>()
val errors = mutableListOf<StatusException>()
forEach { result ->
when (result) {
is Either.Right -> values.add(result.value)
is Either.Left -> errors.add(result.value)
}
}
return Pair(values.asList(), errors.asList())
}
raulraja
09/01/2022, 11:03 PMraulraja
09/01/2022, 11:03 PMseparateEither
mitch
09/01/2022, 11:04 PMraulraja
09/01/2022, 11:04 PMmitch
09/01/2022, 11:05 PMmitch
09/01/2022, 11:05 PMraulraja
09/01/2022, 11:06 PMmitch
09/01/2022, 11:08 PMseparateEither
and using standard library code.
Thanks for the quick help!raulraja
09/01/2022, 11:12 PMmitch
09/01/2022, 11:13 PMGiorgos Makris
09/02/2022, 7:34 AMraulraja
09/02/2022, 7:53 AMGiorgos Makris
09/02/2022, 7:55 AMraulraja
09/02/2022, 7:58 AMGiorgos Makris
09/02/2022, 8:00 AM