unzip



unzips the structure holding the resulting elements in an Pair

import arrow.core.unzip

fun main(args: Array<String>) {
//sampleStart
val result = sequenceOf("A" to 1, "B" to 2).unzip()
//sampleEnd
println("(${result.first}, ${result.second})")
}

fun <A, B, C> Sequence<C>.unzip(fc: (C) -> Pair<A, B>): Pair<Sequence<A>, Sequence<B>>(source)

after applying the given function unzip the resulting structure into its elements.

import arrow.core.unzip

fun main(args: Array<String>) {
//sampleStart
val result =
sequenceOf("A:1", "B:2", "C:3").unzip { e ->
e.split(":").let {
it.first() to it.last()
}
}
//sampleEnd
println("(${result.first}, ${result.second})")
}

fun <K, A, B> Map<K, Pair<A, B>>.unzip(): Pair<Map<K, A>, Map<K, B>>(source)

Unzips the structure holding the resulting elements in an Pair

import arrow.core.*
import io.kotest.matchers.shouldBe

fun test() {
mapOf(
"first" to ("A" to 1),
"second" to ("B" to 2)
).unzip() shouldBe Pair(mapOf("first" to "A", "second" to "B"), mapOf("first" to 1, "second" to 2))
}

inline fun <K, A, B, C> Map<K, C>.unzip(fc: (Map.Entry<K, C>) -> Pair<A, B>): Pair<Map<K, A>, Map<K, B>>(source)

After applying the given function unzip the resulting structure into its elements.

import arrow.core.*
import io.kotest.matchers.shouldBe

fun test() {
mapOf("first" to "A:1", "second" to "B:2", "third" to "C:3").unzip { (_, e) ->
e.split(":").let {
it.first() to it.last()
}
} shouldBe Pair(
mapOf("first" to "A", "second" to "B", "third" to "C"),
mapOf("first" to "1", "second" to "2", "third" to "3")
)
}