align

inline fun <A, B, C> Iterable<A>.align(b: Iterable<B>, fa: (Ior<A, B>) -> C): List<C>

Combines two structures by taking the union of their shapes and combining the elements with the given function.

import arrow.core.*
import io.kotest.matchers.shouldBe

fun test() {
listOf("A", "B").align(listOf(1, 2, 3)) {
"$it"
} shouldBe listOf("Ior.Both(A, 1)", "Ior.Both(B, 2)", "Ior.Right(3)")
}

fun <A, B> Iterable<A>.align(b: Iterable<B>): List<Ior<A, B>>

Combines two structures by taking the union of their shapes and using Ior to hold the elements.

import arrow.core.*
import io.kotest.matchers.shouldBe

fun test() {
listOf("A", "B")
.align(listOf(1, 2, 3)) shouldBe listOf(Ior.Both("A", 1), Ior.Both("B", 2), Ior.Right(3))
}

fun <A, B, C> Sequence<A>.align(seqB: Sequence<B>, fa: (Ior<A, B>) -> C): Sequence<C>

Combines two Sequence by returning Ior.Both when both Sequence have an item, Ior.Left when only the first Sequence has an item, and Ior.Right when only the second Sequence has an item.

import arrow.core.align
import arrow.core.Ior
import arrow.core.Ior.Both
import arrow.core.Ior.Left
import arrow.core.Ior.Right
import io.kotest.matchers.shouldBe

fun test() {
fun Ior<String, Int>.visualise(): String =
fold({ "$it<" }, { ">$it" }, { a, b -> "$a<>$b" })

sequenceOf("A", "B").align(sequenceOf(1, 2, 3)) { ior ->
ior.visualise()
}.toList() shouldBe listOf("A<>1", "B<>2", ">3")

sequenceOf("A", "B", "C").align(sequenceOf(1, 2)) { ior ->
ior.visualise()
}.toList() shouldBe listOf("A<>1", "B<>2", "C<")
}

fun <A, B> Sequence<A>.align(seqB: Sequence<B>): Sequence<Ior<A, B>>

Combines two Sequence by returning Ior.Both when both Sequence have an item, Ior.Left when only the first Sequence has an item, and Ior.Right when only the second Sequence has an item.

import arrow.core.align
import arrow.core.Ior.Both
import arrow.core.Ior.Left
import arrow.core.Ior.Right
import io.kotest.matchers.shouldBe

fun test() {
sequenceOf("A", "B")
.align(sequenceOf(1, 2, 3)).toList() shouldBe listOf(Both("A", 1), Both("B", 2), Right(3))

sequenceOf("A", "B", "C")
.align(sequenceOf(1, 2)).toList() shouldBe listOf(Both("A", 1), Both("B", 2), Left("C"))
}

fun <K, A, B> Map<K, A>.align(b: Map<K, B>): Map<K, Ior<A, B>>

Combines two structures by taking the union of their shapes and using Ior to hold the elements.

import arrow.core.*
import io.kotest.matchers.shouldBe

fun test() {
val res = mapOf(1 to 1, 2 to 2).align(mapOf(1 to "1", 2 to "2", 3 to "3"))
res shouldBe mapOf(1 to Ior.Both(1, "1"), 2 to Ior.Both(2, "2"), 3 to Ior.Right("3"))
}

fun <K, A, B, C> Map<K, A>.align(b: Map<K, B>, fa: (Map.Entry<K, Ior<A, B>>) -> C): Map<K, C>

Combines two structures by taking the union of their shapes and combining the elements with the given function.

import arrow.core.*
import io.kotest.matchers.shouldBe

fun test() {
mapOf("1" to 1, "2" to 2)
.align(mapOf("1" to 1, "2" to 2, "3" to 3)) { (_, a) ->
"$a"
} shouldBe mapOf("1" to "Ior.Both(1, 1)", "2" to Ior.Both(2, 2), "3" to Ior.Right(3))
}