unalign

fun <A, B> Iterable<Ior<A, B>>.unalign(): Pair<List<A?>, List<B?>>


inline fun <A, B, C> Iterable<C>.unalign(fa: (C) -> Ior<A, B>): Pair<List<A?>, List<B?>>

after applying the given function, splits the resulting union shaped structure into its components parts

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

fun test() {
listOf(1, 2, 3, 4).unalign {
if(it % 2 == 0) it.rightIor()
else it.leftIor()
} shouldBe Pair(listOf(1, null, 3, null), listOf(null, 2, null, 4))
}

splits an union into its component parts.

import arrow.core.bothIor
import arrow.core.leftIor
import arrow.core.unalign

fun main() {
//sampleStart
val result = sequenceOf(("A" to 1).bothIor(), ("B" to 2).bothIor(), "C".leftIor()).unalign()
//sampleEnd
println("(${result.first}, ${result.second})")
}

fun <A, B, C> Sequence<C>.unalign(fa: (C) -> Ior<A, B>): Pair<Sequence<A>, Sequence<B>>

after applying the given function, splits the resulting union shaped structure into its components parts

import arrow.core.leftIor
import arrow.core.unalign

fun main() {
//sampleStart
val result = sequenceOf(1, 2, 3).unalign { it.leftIor() }
//sampleEnd
println("(${result.first.toList()}, ${result.second.toList()})")
}

fun <K, A, B> Map<K, Ior<A, B>>.unalign(): Pair<Map<K, A>, Map<K, B>>

Splits a union into its component parts.

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

fun test() {
mapOf(
"first" to Ior.Both("A", 1),
"second" to Ior.Both("B", 2),
"third" to Ior.Left("C")
).unalign() shouldBe Pair(mapOf("first" to "A", "second" to "B", "third" to "C"), mapOf("first" to 1, "second" to 2))
}

inline fun <K, A, B, C> Map<K, C>.unalign(fa: (Map.Entry<K, C>) -> Ior<A, B>): Pair<Map<K, A>, Map<K, B>>

after applying the given function, splits the resulting union shaped structure into its components parts

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

fun test() {
mapOf("1" to 1, "2" to 2, "3" to 3)
.unalign { (key, value) ->
when(key) {
"1" -> Ior.Left(value)
"2" -> Ior.Right(key)
else -> Ior.Both(value, key)
}
} shouldBe Pair(mapOf("1" to 1, "3" to 3), mapOf("2" to 2, "3" to 3))
}