unzip
fun <A, B, C> NonEmptyList<C>.unzip(f: (C) -> Pair<A, B>): Pair<NonEmptyList<A>, NonEmptyList<B>>(source)
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})")
}
Content copied to clipboard
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})")
}
Content copied to clipboard
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))
}
Content copied to clipboard
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")
)
}
Content copied to clipboard