Skip to content

Commit 8061d1f

Browse files
authored
Merge pull request #129 from fhaust/monadzip
Add MonadZip Vector instance
2 parents e22b261 + 6c3c981 commit 8061d1f

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

Data/Vector.hs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ import Control.Monad ( MonadPlus(..), liftM, ap )
173173
import Control.Monad.ST ( ST )
174174
import Control.Monad.Primitive
175175

176+
177+
#if MIN_VERSION_base(4,4,0)
178+
import Control.Monad.Zip
179+
#endif
180+
176181
import Prelude hiding ( length, null,
177182
replicate, (++), concat,
178183
head, last,
@@ -327,6 +332,20 @@ instance MonadPlus Vector where
327332
{-# INLINE mplus #-}
328333
mplus = (++)
329334

335+
-- MonadZip was added in base-4.4.0
336+
#if MIN_VERSION_base(4,4,0)
337+
instance MonadZip Vector where
338+
{-# INLINE mzip #-}
339+
mzip = zip
340+
341+
{-# INLINE mzipWith #-}
342+
mzipWith = zipWith
343+
344+
{-# INLINE munzip #-}
345+
munzip = unzip
346+
#endif
347+
348+
330349
instance Applicative.Applicative Vector where
331350
{-# INLINE pure #-}
332351
pure = singleton

tests/Tests/Vector.hs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ import System.Random (Random)
2727
import Data.Functor.Identity
2828
import Control.Monad.Trans.Writer
2929

30+
#if MIN_VERSION_base(4,4,0)
31+
import Control.Monad.Zip
32+
#endif
33+
3034
#define COMMON_CONTEXT(a, v) \
3135
VANILLA_CONTEXT(a, v), VECTOR_CONTEXT(a, v)
3236

@@ -452,12 +456,23 @@ testPolymorphicFunctions _ = $(testProperties [
452456
constructrN xs n f = constructrN (f xs : xs) (n-1) f
453457

454458
testTuplyFunctions:: forall a v. (COMMON_CONTEXT(a, v), VECTOR_CONTEXT((a, a), v), VECTOR_CONTEXT((a, a, a), v)) => v a -> [Test]
455-
testTuplyFunctions _ = $(testProperties ['prop_zip, 'prop_zip3, 'prop_unzip, 'prop_unzip3])
459+
testTuplyFunctions _ = $(testProperties [ 'prop_zip, 'prop_zip3
460+
, 'prop_unzip, 'prop_unzip3
461+
#if MIN_VERSION_base(4,4,0)
462+
, 'prop_mzip, 'prop_munzip
463+
#endif
464+
])
456465
where
457466
prop_zip :: P (v a -> v a -> v (a, a)) = V.zip `eq` zip
458467
prop_zip3 :: P (v a -> v a -> v a -> v (a, a, a)) = V.zip3 `eq` zip3
459468
prop_unzip :: P (v (a, a) -> (v a, v a)) = V.unzip `eq` unzip
460469
prop_unzip3 :: P (v (a, a, a) -> (v a, v a, v a)) = V.unzip3 `eq` unzip3
470+
#if MIN_VERSION_base(4,4,0)
471+
prop_mzip :: P (Data.Vector.Vector a -> Data.Vector.Vector a -> Data.Vector.Vector (a, a))
472+
= mzip `eq` zip
473+
prop_munzip :: P (Data.Vector.Vector (a, a) -> (Data.Vector.Vector a, Data.Vector.Vector a))
474+
= munzip `eq` unzip
475+
#endif
461476

462477
testOrdFunctions :: forall a v. (COMMON_CONTEXT(a, v), Ord a, Ord (v a)) => v a -> [Test]
463478
testOrdFunctions _ = $(testProperties

0 commit comments

Comments
 (0)