Skip to content

Latest commit

 

History

History
68 lines (45 loc) · 2.22 KB

File metadata and controls

68 lines (45 loc) · 2.22 KB
layout title permalink
docs-core
Semialign
/arrow/typeclasses/semialign/

Semialign

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> that's 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())
}

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"
    }
}

Laws

Arrow provides SemialignLaws{:target="_blank"} 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]({{ '/patterns/glossary' | relative_url }})

Additionally all instances of [Semialign]({{ '/arrow/typeclasses/semialign' | relative_url }}) implement the Functor typeclass directly since they are all subtypes of Functor