partitionEither.kt
# arrow
r
partitionEither.kt
Hi @mitch, We no longer have
Foldable
or those types but I think what you are trying to achieve can be defined as:
Copy code
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
        }
    }
(untested)
m
Thanks, Raul. That makes sense. We weren’t too far off with our thinking (also untested):
Copy code
public 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())
}
separateEither
m
ah hah! I missed that too!
r
I think your impl or the one I proposed it's probably faster than the other one flatMapping twice over the original list
m
Do those create a lot of list objects
yeah
r
yeah, looks like there is room to improve those arrow implementations if we setup benchmarks to prove these other ones are faster and probably more memory effitient.
m
makes sense. Assuming our collections aren’t large, I’m fine with going
separateEither
and using standard library code. Thanks for the quick help!
r
Created an issue so we don't forget to improve those implementations. https://github.com/arrow-kt/arrow/issues/2812
m
Very cool. I think temporary mutability will mean less objects.
g
would grouping the eithers by their class be an acceptable approach performance wise?
r
@Giorgos Makris The general rule we are following in Arrow is that impl can have local mutability and other optimizations as long as they remain legible. I think grouping by class would be acceptable since both subtypes of Either are public.
g
I don't think I have ever contributed to open source projects before but I could give this is a shot in the evening. Would that be alright?
r
Of course and thank you! we would love to see more contributions to Arrow. Feel free to ping us directly with any questions here or in #arrow-contributors if you need any help 🙂
g
awesome! Thank you ^^