diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Iterable.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Iterable.kt index dda5b8fdbb8..c3bd1d2f16d 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Iterable.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Iterable.kt @@ -998,9 +998,16 @@ public fun Iterable>.uniteValidated(): List = * @return a tuple containing List with [Either.Left] and another List with its [Either.Right] values. */ public fun Iterable>.separateEither(): Pair, List> { - val asep = flatMap { gab -> gab.fold({ listOf(it) }, { emptyList() }) } - val bsep = flatMap { gab -> gab.fold({ emptyList() }, { listOf(it) }) } - return asep to bsep + val left = ArrayList(collectionSizeOrDefault(10)) + val right = ArrayList(collectionSizeOrDefault(10)) + + for (either in this) + when (either) { + is Left -> left.add(either.value) + is Right -> right.add(either.value) + } + + return Pair(left, right) } /** @@ -1010,9 +1017,16 @@ public fun Iterable>.separateEither(): Pair, List * @return a tuple containing List with [Validated.Invalid] and another List with its [Validated.Valid] values. */ public fun Iterable>.separateValidated(): Pair, List> { - val asep = flatMap { gab -> gab.fold({ listOf(it) }, { emptyList() }) } - val bsep = flatMap { gab -> gab.fold({ emptyList() }, { listOf(it) }) } - return asep to bsep + val invalids = ArrayList(collectionSizeOrDefault(10)) + val valids = ArrayList(collectionSizeOrDefault(10)) + + for (validated in this) + when (validated) { + is Invalid -> invalids.add(validated.value) + is Valid -> valids.add(validated.value) + } + + return Pair(invalids, valids) } public fun Iterable>.flatten(): List =