Unalign

beginner

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.

Main Combinators

unalign(ior: Kind<F, Ior<A, B»): Tuple2<Kind<F, A>, Kind<F, B»

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())
}
// Tuple2(a=ListK(list=[A, B, C]), b=ListK(list=[1, 2]))

unalignWith(c: Kind<F, C>, fa: (C) -> Ior<A, B>): Tuple2<Kind<F, A>, Kind<F, B»

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()
    }
}
// Tuple2(a=ListK(list=[1, 2, 3]), b=ListK(list=[]))

Laws

Arrow provides UnalignLaws in the form of test cases for internal verification of lawful instances and third party apps creating their own Unalign instances.

Creating your own Unalign instances

Arrow already provides Unalign instances for common datatypes (e.g. Option, ListK, MapK). See their implementations and accomanying 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

Data types

Module Data types
arrow.core ListK, MapK, Option, SequenceK

Type Class Hierarchy