Skip to content

Commit 7ac8238

Browse files
authored
Merge pull request #108 from purescript/iterator-unzip
Faster unzip, part of #71
2 parents bf2c332 + 7e47259 commit 7ac8238

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/Data/Array.purs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,17 @@ zip = zipWith Tuple
671671
-- | Transforms a list of pairs into a list of first components and a list of
672672
-- | second components.
673673
unzip :: forall a b. Array (Tuple a b) -> Tuple (Array a) (Array b)
674-
unzip = uncons' (\_ -> Tuple [] []) \(Tuple a b) ts -> case unzip ts of
675-
Tuple as bs -> Tuple (a : as) (b : bs)
674+
unzip xs =
675+
pureST do
676+
fsts <- emptySTArray
677+
snds <- emptySTArray
678+
iter <- iterator (xs !! _)
679+
iterate iter \(Tuple fst snd) -> do
680+
void $ pushSTArray fsts fst
681+
void $ pushSTArray snds snd
682+
fsts' <- unsafeFreeze fsts
683+
snds' <- unsafeFreeze snds
684+
pure $ Tuple fsts' snds'
676685

677686
-- | Perform a fold using a monadic step function.
678687
foldM :: forall m a b. Monad m => (a -> b -> m a) -> a -> Array b -> m a

0 commit comments

Comments
 (0)