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 c3bd1d2f16d..813eb0e110a 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
@@ -632,7 +632,19 @@ public fun Iterable.rightPadZip(other: Iterable): List>
*
*/
public inline fun Iterable.align(b: Iterable, fa: (Ior) -> C): List =
- this.align(b).map(fa)
+ buildList(maxOf(this.collectionSizeOrDefault(10), b.collectionSizeOrDefault(10))) {
+ val first = this@align.iterator()
+ val second = b.iterator()
+ while (first.hasNext() || second.hasNext()) {
+ val element: Ior = when {
+ first.hasNext() && second.hasNext() -> Ior.Both(first.next(), second.next())
+ first.hasNext() -> first.next().leftIor()
+ second.hasNext() -> second.next().rightIor()
+ else -> throw IllegalStateException("this should never happen")
+ }
+ add(fa(element))
+ }
+ }
/**
* Combines two structures by taking the union of their shapes and using Ior to hold the elements.
@@ -651,18 +663,7 @@ public inline fun Iterable.align(b: Iterable, fa: (Ior) ->
*
*/
public fun Iterable.align(b: Iterable): List> =
- alignRec(this, b)
-
-@Suppress("NAME_SHADOWING")
-private fun alignRec(ls: Iterable, rs: Iterable): List> {
- val ls = if (ls is List) ls else ls.toList()
- val rs = if (rs is List) rs else rs.toList()
- return when {
- ls.isEmpty() -> rs.map { it.rightIor() }
- rs.isEmpty() -> ls.map { it.leftIor() }
- else -> listOf(Ior.Both(ls.first(), rs.first())) + alignRec(ls.drop(1), rs.drop(1))
- }
-}
+ this.align(b, ::identity)
/**
* aligns two structures and combine them with the given [Semigroup.combine]