Skip to content

Commit 2a4f63d

Browse files
committed
Restore zip/unzip
1 parent f77c3c9 commit 2a4f63d

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

bower.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
],
1818
"dependencies": {
1919
"purescript-foldable-traversable": "~0.4.0",
20-
"purescript-st": "~0.1.0"
20+
"purescript-st": "~0.1.0",
21+
"purescript-tuples": "~0.4.0"
2122
},
2223
"devDependencies": {
2324
"purescript-assert": "~0.1.0",

docs/Data.Array.md

+18
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,24 @@ zipWithA :: forall m a b c. (Applicative m) => (a -> b -> m c) -> Array a -> Arr
472472
A generalization of `zipWith` which accumulates results in some `Applicative`
473473
functor.
474474

475+
#### `zip`
476+
477+
``` purescript
478+
zip :: forall a b. Array a -> Array b -> Array (Tuple a b)
479+
```
480+
481+
Rakes two lists and returns a list of corresponding pairs.
482+
If one input list is short, excess elements of the longer list are discarded.
483+
484+
#### `unzip`
485+
486+
``` purescript
487+
unzip :: forall a b. Array (Tuple a b) -> Tuple (Array a) (Array b)
488+
```
489+
490+
Transforms a list of pairs into a list of first components and a list of
491+
second components.
492+
475493
#### `foldM`
476494

477495
``` purescript

src/Data/Array.purs

+15-1
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,23 @@ module Data.Array
6767

6868
, zipWith
6969
, zipWithA
70+
, zip
71+
, unzip
7072

7173
, foldM
7274
) where
7375

7476
import Control.Alt (Alt, (<|>))
7577
import Control.Alternative (Alternative)
78+
import Control.Lazy (Lazy, defer)
7679
import Control.MonadPlus (MonadPlus)
7780
import Control.Plus (Plus)
78-
import Control.Lazy (Lazy, defer)
7981
import Data.Foldable (Foldable, foldr)
8082
import Data.Functor.Invariant (Invariant, imapF)
8183
import Data.Maybe (Maybe(..), maybe, isJust)
8284
import Data.Monoid (Monoid, mempty)
8385
import Data.Traversable (Traversable, traverse, sequence)
86+
import Data.Tuple (Tuple(..))
8487

8588
--------------------------------------------------------------------------------
8689
-- Array creation --------------------------------------------------------------
@@ -456,6 +459,17 @@ foreign import zipWith :: forall a b c. (a -> b -> c) -> Array a -> Array b -> A
456459
zipWithA :: forall m a b c. (Applicative m) => (a -> b -> m c) -> Array a -> Array b -> m (Array c)
457460
zipWithA f xs ys = sequence (zipWith f xs ys)
458461

462+
-- | Rakes two lists and returns a list of corresponding pairs.
463+
-- | If one input list is short, excess elements of the longer list are discarded.
464+
zip :: forall a b. Array a -> Array b -> Array (Tuple a b)
465+
zip = zipWith Tuple
466+
467+
-- | Transforms a list of pairs into a list of first components and a list of
468+
-- | second components.
469+
unzip :: forall a b. Array (Tuple a b) -> Tuple (Array a) (Array b)
470+
unzip = uncons' (\_ -> Tuple [] []) \(Tuple a b) ts -> case unzip ts of
471+
Tuple as bs -> Tuple (a : as) (b : bs)
472+
459473
--------------------------------------------------------------------------------
460474
-- Folding ---------------------------------------------------------------------
461475
--------------------------------------------------------------------------------

test/Test/Data/Array.purs

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Console (log)
44
import Data.Array
55
import Data.Maybe (Maybe(..), isNothing)
66
import Data.Maybe.Unsafe (fromJust)
7+
import Data.Tuple (Tuple(..))
78
import Test.Assert (assert)
89

910
testArray = do
@@ -220,7 +221,15 @@ testArray = do
220221
log "zipWith should use the specified function to zip two lists together"
221222
assert $ zipWith (\x y -> [show x, y]) [1, 2, 3] ["a", "b", "c"] == [["1", "a"], ["2", "b"], ["3", "c"]]
222223

223-
-- zipWithA
224+
log "zipWithA should use the specified function to zip two lists together"
225+
assert $ zipWithA (\x y -> Just $ Tuple x y) [1, 2, 3] ["a", "b", "c"] == Just [Tuple 1 "a", Tuple 2 "b", Tuple 3 "c"]
226+
227+
log "zip should use the specified function to zip two lists together"
228+
assert $ zip [1, 2, 3] ["a", "b", "c"] == [Tuple 1 "a", Tuple 2 "b", Tuple 3 "c"]
229+
230+
log "unzip should deconstruct a list of tuples into a tuple of lists"
231+
assert $ unzip [Tuple 1 "a", Tuple 2 "b", Tuple 3 "c"] == Tuple [1, 2, 3] ["a", "b", "c"]
232+
224233
-- foldM
225234

226235
nil :: Array Int

0 commit comments

Comments
 (0)