unzip

unzip transforms a list of pairs into a list of first components and a list of second components.

Examples

>>> unzip []
([],[])
>>> unzip [(1, 'a'), (2, 'b')]
([1,2],"ab")
Generalization of Data.List.unzip.

Examples

>>> unzip (Just ("Hello", "World"))
(Just "Hello",Just "World")
>>> unzip [("I", "love"), ("really", "haskell")]
(["I","really"],["love","haskell"])
Warning: This function will be made monomorphic in base-4.22, consider switching to Data.Functor.unzip
O(n) unzip transforms a list of pairs of bytes into a pair of ByteStrings. Note that this performs two pack operations.
unzip transforms a list of pairs of Chars into a pair of ByteStrings. Note that this performs two pack operations.
O(n) unzip transforms a list of pairs of chars into a pair of ByteStrings. Note that this performs two pack operations.
Unzip a sequence of pairs.
unzip ps = ps `seq` (fmap fst ps) (fmap snd ps)
Example:
unzip $ fromList [(1,"a"), (2,"b"), (3,"c")] =
(fromList [1,2,3], fromList ["a", "b", "c"])
See the note about efficiency at unzipWith.
unzip transforms a list of pairs into a list of first components and a list of second components.
>>> unzip []
([],[])

>>> unzip [(1, 'a'), (2, 'b')]
([1,2],"ab")
Generalization of Data.List.unzip. Since: 4.19.0.0
The unzip function is the inverse of the zip function.
Caution: Every pair member has a reference to the argument of unzip. Depending on the consumption pattern this may cause a memory leak. For lists, I think, you should generally prefer unzip.
Like standard unzip but more lazy. It is Data.List.unzip undefined == undefined, but unzip undefined == (undefined, undefined).
Unzip for stict pairs into a (lazy) pair of lists.
Takes apart a stream of pairs, producing a pair of input streams. Reading from either of the produced streams will cause a pair of values to be pulled from the original stream if necessary. Note that reading n values from one of the returned streams will cause n values to be buffered at the other stream. Access to the original stream is thread safe, i.e. guarded by a lock.
The type
Data.List.unzip     :: [(a,b)] -> ([a],[b])
might lead us to expect
Streaming.unzip :: Stream (Of (a,b)) m r -> Stream (Of a) m (Stream (Of b) m r)
which would not stream, since it would have to accumulate the second stream (of bs). Of course, Data.List unzip doesn't stream either. This unzip does stream, though of course you can spoil this by using e.g. toList:
>>> let xs = Prelude.map (\x -> (x, Prelude.show x)) [1..5 :: Int]
>>> S.toList $ S.toList $ S.unzip (S.each xs)
["1","2","3","4","5"] :> ([1,2,3,4,5] :> ())
>>> Prelude.unzip xs
([1,2,3,4,5],["1","2","3","4","5"])
Note the difference of order in the results. It may be of some use to think why. The first application of toList was applied to a stream of integers:
>>> :t S.unzip $ S.each xs
S.unzip $ S.each xs :: Monad m => Stream (Of Int) (Stream (Of String) m) ()
Like any fold, toList takes no notice of the monad of effects.
toList :: Monad m => Stream (Of a) m r -> m (Of [a] r)
In the case at hand (since I am in ghci) m = Stream (Of String) IO. So when I apply toList, I exhaust that stream of integers, folding it into a list:
>>> :t S.toList $ S.unzip $ S.each xs
S.toList $ S.unzip $ S.each xs
:: Monad m => Stream (Of String) m (Of [Int] ())
When I apply toList to this, I reduce everything to an ordinary action in IO, and return a list of strings:
>>> S.toList $ S.toList $ S.unzip (S.each xs)
["1","2","3","4","5"] :> ([1,2,3,4,5] :> ())
unzip can be considered a special case of either unzips or expand:
unzip = unzips . maps (\((a,b) :> x) -> Compose (a :> b :> x))
unzip = expand $ \p ((a,b) :> abs) -> b :> p (a :> abs)
unzip transforms a list of pairs into a list of first components and a list of second components.
O(min(m,n)) Unzip a vector of pairs.
O(min(m,n)) Unzip a vector of pairs.
O(1) Unzip 2 vectors
O(1). Unzip an unboxed array.
Converts a list of pairs into two separate lists of elements