unalign
splits an union into its component parts.
import arrow.core.bothIor
import arrow.core.leftIor
import arrow.core.unalign
fun main(args: Array<String>) {
//sampleStart
val result = sequenceOf(("A" to 1).bothIor(), ("B" to 2).bothIor(), "C".leftIor()).unalign()
//sampleEnd
println("(${result.first}, ${result.second})")
}
Content copied to clipboard
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(args: Array<String>) {
//sampleStart
val result = sequenceOf(1, 2, 3).unalign { it.leftIor() }
//sampleEnd
println("(${result.first.toList()}, ${result.second.toList()})")
}
Content copied to clipboard
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))
}
Content copied to clipboard
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>>(source)
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))
}
Content copied to clipboard