The Unlign
typeclass extends the Semialign
typeclass with an inverse function to align: It splits a union shape
into a tuple representing the component parts.
splits a union into its component parts.
import arrow.core.extensions.*
import arrow.core.extensions.listk.unalign.unalign
import arrow.core.*
ListK.unalign().run {
unalign(listOf(("A" toT 1).bothIor(), ("B" toT 2).bothIor(), "C".leftIor()).k())
}
// ([A, B, C], [1, 2])
after applying the given function, splits the resulting union shaped structure into its components parts
import arrow.core.extensions.*
import arrow.core.extensions.listk.unalign.unalign
import arrow.core.*
ListK.unalign().run {
unalignWith(listOf(1, 2, 3).k()) {
it.leftIor()
}
}
// ([1, 2, 3], [])
Arrow provides UnalignLaws
in the form of test cases for internal verification of lawful instances and third party apps creating their own Unalign instances.
Unalign
instancesArrow already provides Unalign instances for common datatypes (e.g. Option, ListK, MapK). See their implementations and accompanying testcases for reference.
See Deriving and creating custom typeclass
Additionally all instances of Unalign
implement the Semialign
typeclass directly
since they are all subtypes of Semialign
Module | Data types |
arrow.core | ListK, MapK, Option, SequenceK |
Do you like Arrow?
✖