Zip

The Zip typeclass extends the Semialign typeclass with a function that takes the intersection of non-uniform shapes.

Main Combinators

zip

Combines two structures by taking the intersection of their shapes and using Tuple2 to hold the elements.

fun <A, B> Kind<F, A>.zip(other: Kind<F, B>): Kind<F, Tuple2<A, B>>

import arrow.core.extensions.*
import arrow.core.extensions.listk.zip.zip
import arrow.core.*

ListK.zip().run {
    listOf("A", "B").k().zip(listOf(1, 2, 3).k())
}
// [(A, 1), (B, 2)]

zipWith

Combines two structures by taking the intersection of their shapes and then combines the elements with the given function.

fun <A, B, C> Kind<F, A>.zipWith(other: Kind<F, B>, f: (A, B) -> C): Kind<F, C>

import arrow.core.extensions.*
import arrow.core.extensions.listk.zip.zip
import arrow.core.*

ListK.zip().run {
    listOf("A", "B").k().zipWith(listOf(1, 2, 3).k()) {
        a, b -> "$a # $b"
    }
}
// [A # 1, B # 2]

Laws

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

Creating your own Zip instances

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

Data types

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

Type Class Hierarchy

Do you like Arrow?

Arrow Org
<