unalign
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))
}
Content copied to clipboard
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})")
}
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() {
//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>>
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