on master help sign in
Get rid of most of findIndex, findIndices, elemIndex, and elemIndices.

--HG-- extra : convert_revision : 44894b243b0771f84f8e58d548050b3c55d4c7b0

commite0ef874e25b06bbb7b1556fd354ac16529f17f4f authorBryan O'Sullivan <bos@serpentine.com> dateTue 6 Oct 2009, 20:30 (17 years ago) parenta205d6a
6 files changed−171
Data/Text.hs−24 Data/Text/Fusion.hs−37 Data/Text/Fusion/Common.hs−24 Data/Text/Lazy.hs−33 Data/Text/Lazy/Fusion.hs−31 tests/Properties.hs−22
Data/Text.hs−24
@@ -154,9 +154,6 @@ module Data.Text
154154 -- $index
155155 , index
156156 , findIndex
157− , findIndices
158− , elemIndex
159− , elemIndices
160157 , count
161158
162159 -- * Zipping and unzipping
@@ -1117,27 +1114,6 @@ findIndex :: (Char -> Bool) -> Text -> Maybe Int
11171114findIndex p t = S.findIndex p (stream t)
11181115{-# INLINE findIndex #-}
11191116
1120−-- | The 'findIndices' function extends 'findIndex', by returning the
1121−-- indices of all elements satisfying the predicate, in ascending
1122−-- order. Subject to fusion.
1123−findIndices :: (Char -> Bool) -> Text -> [Int]
1124−findIndices p t = S.findIndices p (stream t)
1125−{-# INLINE findIndices #-}
1126−
1127−-- | /O(n)/ The 'elemIndex' function returns the index of the first
1128−-- element in the given 'Text' which is equal to the query element, or
1129−-- 'Nothing' if there is no such element. Subject to fusion.
1130−elemIndex :: Char -> Text -> Maybe Int
1131−elemIndex c t = S.elemIndex c (stream t)
1132−{-# INLINE elemIndex #-}
1133−
1134−-- | /O(n)/ The 'elemIndices' function returns the index of every
1135−-- element in the given 'Text' which is equal to the query
1136−-- element. Subject to fusion.
1137−elemIndices :: Char -> Text -> [Int]
1138−elemIndices c t = S.elemIndices c (stream t)
1139−{-# INLINE elemIndices #-}
1140−
11411117-- | /O(n+m)/ The 'count' function returns the number of times the
11421118-- query string appears in the given 'Text'. An empty query string is
11431119-- invalid, and will cause an error to be raised.
Data/Text/Fusion.hs−37
@@ -40,10 +40,6 @@ module Data.Text.Fusion
4040 -- * Indexing
4141 , index
4242 , findIndex
43− , findIndices
44− , findIndexOrEnd
45− , elemIndex
46− , elemIndices
4743 , countChar
4844 ) where
4945
@@ -195,39 +191,6 @@ findIndex :: (Char -> Bool) -> Stream Char -> Maybe Int
195191findIndex = S.findIndexI
196192{-# INLINE [0] findIndex #-}
197193
198−-- | The 'findIndices' function takes a predicate and a stream and
199−-- returns all indices of the elements in the stream
200−-- satisfying the predicate.
201−findIndices :: (Char -> Bool) -> Stream Char -> [Int]
202−findIndices = S.findIndicesI
203−{-# INLINE [0] findIndices #-}
204−
205−-- | The 'findIndexOrEnd' function takes a predicate and a stream and
206−-- returns the index of the first element in the stream
207−-- satisfying the predicate.
208−findIndexOrEnd :: (Char -> Bool) -> Stream Char -> Int
209−findIndexOrEnd p (Stream next s0 _len) = loop_findIndex 0 s0
210− where
211− loop_findIndex !i !s = case next s of
212− Done -> i
213− Skip s' -> loop_findIndex i s' -- hmm. not caught by QC
214− Yield x s' | p x -> i
215− | otherwise -> loop_findIndex (i+1) s'
216−{-# INLINE [0] findIndexOrEnd #-}
217−
218−-- | /O(n)/ The 'elemIndex' function returns the index of the first
219−-- element in the given stream which is equal to the query
220−-- element, or 'Nothing' if there is no such element.
221−elemIndex :: Char -> Stream Char -> Maybe Int
222−elemIndex = S.elemIndexI
223−{-# INLINE [0] elemIndex #-}
224−
225−-- | /O(n)/ The 'elemIndices' function returns the index of every
226−-- element in the given stream which is equal to the query element.
227−elemIndices :: Char -> Stream Char -> [Int]
228−elemIndices = S.elemIndicesI
229−{-# INLINE [0] elemIndices #-}
230−
231194-- | /O(n)/ The 'count' function returns the number of times the query
232195-- element appears in the given stream.
233196countChar :: Char -> Stream Char -> Int
Data/Text/Fusion/Common.hs−24
@@ -92,9 +92,6 @@ module Data.Text.Fusion.Common
9292 , findBy
9393 , indexI
9494 , findIndexI
95− , findIndicesI
96− , elemIndexI
97− , elemIndicesI
9895 , countCharI
9996
10097 -- * Zipping and unzipping
@@ -831,27 +828,6 @@ zipWith f (Stream next0 sa0 len1) (Stream next1 sb0 len2) =
831828 Yield b sb' -> Yield (f a b) (sa' :!: sb' :!: N)
832829{-# INLINE [0] zipWith #-}
833830
834−-- | /O(n)/ The 'elemIndexI' function returns the index of the first
835−-- element in the given stream which is equal to the query
836−-- element, or 'Nothing' if there is no such element.
837−elemIndexI :: Integral a => Char -> Stream Char -> Maybe a
838−elemIndexI a s = case elemIndicesI a s of
839− (i:_) -> Just i
840− _ -> Nothing
841−{-# INLINE [0] elemIndexI #-}
842−
843−-- | /O(n)/ The 'elemIndicesI' function returns the index of every
844−-- element in the given stream which is equal to the query element.
845−elemIndicesI :: Integral a => Char -> Stream Char -> [a]
846−elemIndicesI a (Stream next s0 _len) = loop 0 s0
847− where
848− loop !i !s = case next s of
849− Done -> []
850− Skip s' -> loop i s'
851− Yield x s' | a == x -> i : loop (i+1) s'
852− | otherwise -> loop (i+1) s'
853−{-# INLINE [0] elemIndicesI #-}
854−
855831-- | /O(n)/ The 'countCharI' function returns the number of times the
856832-- query element appears in the given stream.
857833countCharI :: Integral a => Char -> Stream Char -> a
Data/Text/Lazy.hs−33
@@ -155,10 +155,6 @@ module Data.Text.Lazy
155155
156156 -- * Indexing
157157 , index
158− , findIndex
159− , findIndices
160− , elemIndex
161− , elemIndices
162158 , count
163159
164160 -- * Zipping and unzipping
@@ -1085,35 +1081,6 @@ index :: Text -> Int64 -> Char
10851081index t n = S.index (stream t) n
10861082{-# INLINE index #-}
10871083
1088−-- | /O(n)/ The 'findIndex' function takes a predicate and a 'Text'
1089−-- and returns the index of the first element in the 'Text' satisfying
1090−-- the predicate. This function is subject to fusion.
1091−findIndex :: (Char -> Bool) -> Text -> Maybe Int64
1092−findIndex p t = S.findIndex p (stream t)
1093−{-# INLINE findIndex #-}
1094−
1095−-- | The 'findIndices' function extends 'findIndex', by returning the
1096−-- indices of all elements satisfying the predicate, in ascending
1097−-- order. This function is subject to fusion.
1098−findIndices :: (Char -> Bool) -> Text -> [Int64]
1099−findIndices p t = S.findIndices p (stream t)
1100−{-# INLINE findIndices #-}
1101−
1102−-- | /O(n)/ The 'elemIndex' function returns the index of the first
1103−-- element in the given 'Text' which is equal to the query element, or
1104−-- 'Nothing' if there is no such element. This function is subject to
1105−-- fusion.
1106−elemIndex :: Char -> Text -> Maybe Int64
1107−elemIndex c t = S.elemIndex c (stream t)
1108−{-# INLINE elemIndex #-}
1109−
1110−-- | /O(n)/ The 'elemIndices' function returns the index of every
1111−-- element in the given 'Text' which is equal to the query
1112−-- element. This function is subject to fusion.
1113−elemIndices :: Char -> Text -> [Int64]
1114−elemIndices c t = S.elemIndices c (stream t)
1115−{-# INLINE elemIndices #-}
1116−
11171084-- | /O(n*m)/ The 'count' function returns the number of times the
11181085-- query string appears in the given 'Text'. An empty query string is
11191086-- invalid, and will cause an error to be raised.
Data/Text/Lazy/Fusion.hs−31
@@ -18,10 +18,6 @@ module Data.Text.Lazy.Fusion
1818 , length
1919 , unfoldrN
2020 , index
21− , findIndex
22− , findIndices
23− , elemIndex
24− , elemIndices
2521 , countChar
2622 ) where
2723
@@ -106,33 +102,6 @@ index :: Stream Char -> Int64 -> Char
106102index = S.indexI
107103{-# INLINE [0] index #-}
108104
109−-- | The 'findIndex' function takes a predicate and a stream and
110−-- returns the index of the first element in the stream
111−-- satisfying the predicate.
112−findIndex :: (Char -> Bool) -> Stream Char -> Maybe Int64
113−findIndex = S.findIndexI
114−{-# INLINE [0] findIndex #-}
115−
116−-- | The 'findIndices' function takes a predicate and a stream and
117−-- returns all indices of the elements in the stream
118−-- satisfying the predicate.
119−findIndices :: (Char -> Bool) -> Stream Char -> [Int64]
120−findIndices = S.findIndicesI
121−{-# INLINE [0] findIndices #-}
122−
123−-- | /O(n)/ The 'elemIndex' function returns the index of the first
124−-- element in the given stream which is equal to the query
125−-- element, or 'Nothing' if there is no such element.
126−elemIndex :: Char -> Stream Char -> Maybe Int64
127−elemIndex = S.elemIndexI
128−{-# INLINE [0] elemIndex #-}
129−
130−-- | /O(n)/ The 'elemIndices' function returns the index of every
131−-- element in the given stream which is equal to the query element.
132−elemIndices :: Char -> Stream Char -> [Int64]
133−elemIndices = S.elemIndicesI
134−{-# INLINE [0] elemIndices #-}
135−
136105-- | /O(n)/ The 'count' function returns the number of times the query
137106-- element appears in the given stream.
138107countChar :: Char -> Stream Char -> Int64
tests/Properties.hs−22
@@ -492,18 +492,7 @@ t_index s = forAll (choose (-l,l*2)) ((s L.!!) `eq` T.index (packS s))
492492tl_index s = forAll (choose (-l,l*2)) ((s L.!!) `eq` (TL.index (packS s) . fromIntegral))
493493 where l = L.length s
494494
495−sf_findIndex q p = (L.findIndex p . L.filter q) `eqP` (S.findIndex p . S.filter q)
496495t_findIndex p = L.findIndex p `eqP` T.findIndex p
497−tl_findIndex p = (fmap fromIntegral . L.findIndex p) `eqP` TL.findIndex p
498−sf_findIndices q p= (L.findIndices p . L.filter q) `eqP` (S.findIndices p . S.filter q)
499−t_findIndices p = L.findIndices p `eqP` T.findIndices p
500−tl_findIndices p = (fmap fromIntegral . L.findIndices p) `eqP` TL.findIndices p
501−sf_elemIndex p c = (L.elemIndex c . L.filter p) `eqP` (S.elemIndex c . S.filter p)
502−t_elemIndex c = L.elemIndex c `eqP` T.elemIndex c
503−tl_elemIndex c = (fmap fromIntegral . L.elemIndex c) `eqP` TL.elemIndex c
504−sf_elemIndices p c= (L.elemIndices c . L.filter p) `eqP` (S.elemIndices c . S.filter p)
505−t_elemIndices c = L.elemIndices c`eqP` T.elemIndices c
506−tl_elemIndices c = (fmap fromIntegral . L.elemIndices c) `eqP` TL.elemIndices c
507496t_count t = (subtract 1 . L.length . T.split t) `eq` T.count t
508497tl_count t = (subtract 1 . L.genericLength . TL.split t) `eq` TL.count t
509498t_zip s = L.zip s `eqP` T.zip (packS s)
@@ -869,18 +858,7 @@ tests = [
869858 testProperty "sf_index" sf_index,
870859 testProperty "t_index" t_index,
871860 testProperty "tl_index" tl_index,
872− testProperty "sf_findIndex" sf_findIndex,
873861 testProperty "t_findIndex" t_findIndex,
874− testProperty "tl_findIndex" tl_findIndex,
875− testProperty "sf_findIndices" sf_findIndices,
876− testProperty "t_findIndices" t_findIndices,
877− testProperty "tl_findIndices" tl_findIndices,
878− testProperty "sf_elemIndex" sf_elemIndex,
879− testProperty "t_elemIndex" t_elemIndex,
880− testProperty "tl_elemIndex" tl_elemIndex,
881− testProperty "sf_elemIndices" sf_elemIndices,
882− testProperty "t_elemIndices" t_elemIndices,
883− testProperty "tl_elemIndices" tl_elemIndices,
884862 testProperty "t_count" t_count,
885863 testProperty "tl_count" tl_count,
886864 testProperty "t_indices" t_indices,