Semialign

beginner

The Semialign typeclass lets us combine two structures of type Kind<F, A> and Kind<F, B> into a single type Kind<F, Ior<A,B>>. The type Ior<A,B> thats used to hold the elements allows either side to be absent. This allows to combine the two structures without truncating to the size of the smaller input.

Main Combinators

align

Combines two structures by taking the union of their shapes and using Ior to hold the elements.

fun <A, B> align(left: Kind<F, A>, right: Kind<F, B>): Kind<F, Ior<A, B>>


import arrow.core.extensions.*
import arrow.core.extensions.listk.semialign.semialign
import arrow.core.*

ListK.semialign().run {
    align(listOf("A", "B").k(), listOf(1, 2, 3).k())
}
// ListK(list=[Both(leftValue=A, rightValue=1), Both(leftValue=B, rightValue=2), Right(value=3)])

alignWith

combines two structures by taking the union of their shapes and combining the elements with the given function.

fun <A, B, C> alignWith(a: Kind<F, A>, b: Kind<F, B>, fa: (Ior<A, B>) -> C): Kind<F, C>


import arrow.core.extensions.*
import arrow.core.extensions.listk.semialign.semialign
import arrow.core.*

ListK.semialign().run {
    alignWith(listOf("A", "B").k(), listOf(1, 2, 3).k()) {
        "$it"
    }
}
// ListK(list=[Both(leftValue=A, rightValue=1), Both(leftValue=B, rightValue=2), Right(value=3)])

Laws

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

Creating your own Semialign instances

Arrow already provides Semialign 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 Semialign implement the Functor typeclass directly since they are all subtypes of Functor

Data types

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

Type Class Hierarchy