on master help sign in
Implement and test strict and lazy zip

--HG-- extra : convert_revision : 26d0db1af95bf3e98938927b481c39b3ecabadb0

commitb846b5ec1c65bbcd1dbca3c481771504f3b676a6 authorBryan O'Sullivan <bos@serpentine.com> dateSun 8 Mar 2009, 05:57 (18 years ago) parent0797072
4 files changed+25−4
Data/Text.hs+9 Data/Text/Fusion/Common.hs+1−1 Data/Text/Lazy.hs+11−3 tests/Properties.hs+4
Data/Text.hs+9
@@ -137,6 +137,7 @@ module Data.Text
137137 , count
138138
139139 -- * Zipping and unzipping
140+ , zip
140141 , zipWith
141142
142143 -- -* Ordered text
@@ -864,6 +865,14 @@ count c t = S.count c (stream t)
864865-------------------------------------------------------------------------------
865866-- * Zipping
866867
868+-- | /O(n)/ 'zip' takes two 'Text's and returns a list of
869+-- corresponding pairs of bytes. If one input 'Text' is short,
870+-- excess elements of the longer 'Text' are discarded. This is
871+-- equivalent to a pair of 'unpack' operations.
872+zip :: Text -> Text -> [(Char,Char)]
873+zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
874+{-# INLINE [0] zip #-}
875+
867876-- | /O(n)/ 'zipWith' generalises 'zip' by zipping with the function
868877-- given as the first argument, instead of a tupling function.
869878zipWith :: (Char -> Char -> Char) -> Text -> Text -> Text
Data/Text/Fusion/Common.hs+1−1
@@ -700,7 +700,7 @@ findIndicesI p (Stream next s0 _len) = loop_findIndex 0 s0
700700
701701-- | zipWith generalises 'zip' by zipping with the function given as
702702-- the first argument, instead of a tupling function.
703−zipWith :: (Char -> Char -> Char) -> Stream Char -> Stream Char -> Stream Char
703+zipWith :: (a -> a -> b) -> Stream a -> Stream a -> Stream b
704704zipWith f (Stream next0 sa0 len1) (Stream next1 sb0 len2) = Stream next (sa0 :!: sb0 :!: Nothing) (min len1 len2)
705705 where
706706 {-# INLINE next #-}
Data/Text/Lazy.hs+11−3
@@ -141,6 +141,7 @@ module Data.Text.Lazy
141141 , count
142142
143143 -- * Zipping and unzipping
144+ , zip
144145 , zipWith
145146
146147 -- -* Ordered text
@@ -148,9 +149,8 @@ module Data.Text.Lazy
148149 ) where
149150
150151import Prelude (Char, Bool(..), Int, Maybe(..), String,
151− Eq(..), Ord(..), (++),
152− Read(..), Show(..),
153− (&&), (+), (-), (.),
152+ Eq(..), Ord(..), Read(..), Show(..),
153+ (&&), (+), (-), (.), ($), (++),
154154 flip, fromIntegral, not, otherwise)
155155import qualified Prelude as P
156156import Data.Int (Int64)
@@ -850,6 +850,14 @@ count :: Char -> Text -> Int64
850850count c t = S.count c (stream t)
851851{-# INLINE count #-}
852852
853+-- | /O(n)/ 'zip' takes two 'Text's and returns a list of
854+-- corresponding pairs of bytes. If one input 'Text' is short,
855+-- excess elements of the longer 'Text' are discarded. This is
856+-- equivalent to a pair of 'unpack' operations.
857+zip :: Text -> Text -> [(Char,Char)]
858+zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
859+{-# INLINE [0] zip #-}
860+
853861-- | /O(n)/ 'zipWith' generalises 'zip' by zipping with the function
854862-- given as the first argument, instead of a tupling function.
855863zipWith :: (Char -> Char -> Char) -> Text -> Text -> Text
tests/Properties.hs+4
@@ -331,6 +331,8 @@ prop_T_elemIndices c = L.elemIndices c`eqP` T.elemIndices c
331331prop_TL_elemIndices c = (fmap fromIntegral . L.elemIndices c) `eqP` TL.elemIndices c
332332prop_T_count c = (L.length . L.elemIndices c) `eqP` T.count c
333333prop_TL_count c = (fromIntegral . L.length . L.elemIndices c) `eqP` TL.count c
334+prop_T_zip s = L.zip s `eqP` T.zip (packS s)
335+prop_TL_zip s = L.zip s `eqP` TL.zip (packS s)
334336prop_T_zipWith c s = L.zipWith c s `eqP` (unpackS . T.zipWith c (packS s))
335337prop_TL_zipWith c s = L.zipWith c s `eqP` (unpackS . TL.zipWith c (packS s))
336338
@@ -539,6 +541,8 @@ tests = [
539541 ("prop_TL_elemIndices", mytest prop_TL_elemIndices),
540542 ("prop_T_count", mytest prop_T_count),
541543 ("prop_TL_count", mytest prop_TL_count),
544+ ("prop_T_zip", mytest prop_T_zip),
545+ ("prop_TL_zip", mytest prop_TL_zip),
542546 ("prop_T_zipWith", mytest prop_T_zipWith),
543547 ("prop_TL_zipWith", mytest prop_TL_zipWith),
544548