Commits

Bryan O'Sullivan committed 1fd0e54

Rename span to spanBy, break to breakBy, and partition to partitionBy.

Comments (0)

Files changed (3)

     , stripStart
     , stripEnd
     , splitAt
-    , span
-    , break
+    , spanBy
+    , breakBy
     , group
     , groupBy
     , inits
     , filter
     , find
     , findBy
-    , partition
+    , partitionBy
 
     -- , findSubstring
     
             where d                = iter_ t i
 {-# INLINE splitAt #-}
 
--- | /O(n)/ 'span', applied to a predicate @p@ and text @t@, returns a
--- pair whose first element is the longest prefix (possibly empty) of
--- @t@ of elements that satisfy @p@, and whose second is the remainder
--- of the list.
-span :: (Char -> Bool) -> Text -> (Text, Text)
-span p t@(Text arr off len) = (textP arr off k, textP arr (off+k) (len-k))
+-- | /O(n)/ 'spanBy', applied to a predicate @p@ and text @t@, returns
+-- a pair whose first element is the longest prefix (possibly empty)
+-- of @t@ of elements that satisfy @p@, and whose second is the
+-- remainder of the list.
+spanBy :: (Char -> Bool) -> Text -> (Text, Text)
+spanBy p t@(Text arr off len) = (textP arr off k, textP arr (off+k) (len-k))
   where k = loop 0
         loop !i | i >= len || not (p c) = i
                 | otherwise             = loop (i+d)
             where (c,d)                 = iter t i
-{-# INLINE span #-}
+{-# INLINE spanBy #-}
 
--- | /O(n)/ 'break' is like 'span', but the prefix returned is over
--- elements that fail the predicate @p@.
-break :: (Char -> Bool) -> Text -> (Text, Text)
-break p = span (not . p)
-{-# INLINE break #-}
+-- | /O(n)/ 'breakBy' is like 'spanBy', but the prefix returned is
+-- over elements that fail the predicate @p@.
+breakBy :: (Char -> Bool) -> Text -> (Text, Text)
+breakBy p = spanBy (not . p)
+{-# INLINE breakBy #-}
 
 -- | /O(n)/ Group characters in a string according to a predicate.
 groupBy :: (Char -> Char -> Bool) -> Text -> [Text]
 splitBy p t = loop t
     where loop s | null s'   = [l]
                  | otherwise = l : loop (unsafeTail s')
-              where (l, s') = break p s
+              where (l, s') = breakBy p s
 {-# INLINE splitBy #-}
 
 -- | /O(n)/ Splits a 'Text' into components of length @k@.  The last
 findBy p t = S.findBy p (stream t)
 {-# INLINE findBy #-}
 
--- | /O(n)/ The 'partition' function takes a predicate and a 'Text',
+-- | /O(n)/ The 'partitionBy' function takes a predicate and a 'Text',
 -- and returns the pair of 'Text's with elements which do and do not
 -- satisfy the predicate, respectively; i.e.
 --
--- > partition p t == (filter p t, filter (not . p) t)
-partition :: (Char -> Bool) -> Text -> (Text, Text)
-partition p t = (filter p t, filter (not . p) t)
-{-# INLINE partition #-}
+-- > partitionBy p t == (filter p t, filter (not . p) t)
+partitionBy :: (Char -> Bool) -> Text -> (Text, Text)
+partitionBy p t = (filter p t, filter (not . p) t)
+{-# INLINE partitionBy #-}
 
 -- | /O(n)/ 'filter', applied to a predicate and a 'Text',
 -- returns a 'Text' containing those characters that satisfy the
          | otherwise = h : if null t
                            then []
                            else lines (unsafeTail t)
-    where (h,t) = span (/= '\n') ps
+    where (h,t) = spanBy (/= '\n') ps
 {-# INLINE lines #-}
 
 -- | /O(n)/ Portably breaks a 'Text' up into a list of 'Text's at line

Data/Text/Lazy.hs

     , stripStart
     , stripEnd
     , splitAt
-    , span
-    , break
+    , spanBy
+    , breakBy
     , group
     , groupBy
     , inits
     , elem
     , filter
     , findBy
-    , partition
+    , partitionBy
 
     -- , findSubstring
     
                            in (Chunk t ts', ts'')
              where len = fromIntegral (T.length t)
 
--- | /O(n)/ 'break' is like 'span', but the prefix returned is over
+-- | /O(n)/ 'breakBy' is like 'spanBy', but the prefix returned is over
 -- elements that fail the predicate @p@.
-break :: (Char -> Bool) -> Text -> (Text, Text)
-break p t0 = break' t0
+breakBy :: (Char -> Bool) -> Text -> (Text, Text)
+breakBy p t0 = break' t0
   where break' Empty          = (empty, empty)
         break' c@(Chunk t ts) =
           case T.findIndex p t of
                    | otherwise -> let (a,b) = T.splitAt n t
                                   in (Chunk a Empty, Chunk b ts)
 
--- | /O(n)/ 'span', applied to a predicate @p@ and text @t@, returns a
--- pair whose first element is the longest prefix (possibly empty) of
--- @t@ of elements that satisfy @p@, and whose second is the remainder
--- of the list.
-span :: (Char -> Bool) -> Text -> (Text, Text)
-span p = break (not . p)
-{-# INLINE span #-}
+-- | /O(n)/ 'spanBy', applied to a predicate @p@ and text @t@, returns
+-- a pair whose first element is the longest prefix (possibly empty)
+-- of @t@ of elements that satisfy @p@, and whose second is the
+-- remainder of the list.
+spanBy :: (Char -> Bool) -> Text -> (Text, Text)
+spanBy p = breakBy (not . p)
+{-# INLINE spanBy #-}
 
 -- | The 'group' function takes a 'Text' and returns a list of 'Text's
 -- such that the concatenation of the result is equal to the argument.
 groupBy :: (Char -> Char -> Bool) -> Text -> [Text]
 groupBy _  Empty        = []
 groupBy eq (Chunk t ts) = cons x ys : groupBy eq zs
-                          where (ys,zs) = span (eq x) xs
+                          where (ys,zs) = spanBy (eq x) xs
                                 x  = T.unsafeHead t
                                 xs = chunk (T.unsafeTail t) ts
 
 -- newline 'Char's. The resulting strings do not contain newlines.
 lines :: Text -> [Text]
 lines Empty = []
-lines t = let (l,t') = break ((==) '\n') t
+lines t = let (l,t') = breakBy ((==) '\n') t
           in l : if null t' then []
                  else lines (tail t')
 
 findBy p t = S.findBy p (stream t)
 {-# INLINE findBy #-}
 
--- | /O(n)/ The 'partition' function takes a predicate and a 'Text',
+-- | /O(n)/ The 'partitionBy' function takes a predicate and a 'Text',
 -- and returns the pair of 'Text's with elements which do and do not
 -- satisfy the predicate, respectively; i.e.
 --
--- > partition p t == (filter p t, filter (not . p) t)
-partition :: (Char -> Bool) -> Text -> (Text, Text)
-partition p t = (filter p t, filter (not . p) t)
-{-# INLINE partition #-}
+-- > partitionBy p t == (filter p t, filter (not . p) t)
+partitionBy :: (Char -> Bool) -> Text -> (Text, Text)
+partitionBy p t = (filter p t, filter (not . p) t)
+{-# INLINE partitionBy #-}
 
 -- | /O(n)/ 'Text' index (subscript) operator, starting from 0.
 index :: Text -> Int64 -> Char

tests/Properties.hs

 tl_strip          = TL.dropAround isSpace `eq` TL.strip
 t_splitAt n       = L.splitAt n   `eqP` (unpack2 . T.splitAt n)
 tl_splitAt n      = L.splitAt n   `eqP` (unpack2 . TL.splitAt (fromIntegral n))
-t_span p          = L.span p      `eqP` (unpack2 . T.span p)
-tl_span p         = L.span p      `eqP` (unpack2 . TL.span p)
-t_break p         = L.break p     `eqP` (unpack2 . T.break p)
-tl_break p        = L.break p     `eqP` (unpack2 . TL.break p)
+t_span p          = L.span p      `eqP` (unpack2 . T.spanBy p)
+tl_span p         = L.span p      `eqP` (unpack2 . TL.spanBy p)
+t_break p         = L.break p     `eqP` (unpack2 . T.breakBy p)
+tl_break p        = L.break p     `eqP` (unpack2 . TL.breakBy p)
 t_group           = L.group       `eqP` (map unpackS . T.group)
 tl_group          = L.group       `eqP` (map unpackS . TL.group)
 t_groupBy p       = L.groupBy p   `eqP` (map unpackS . T.groupBy p)
 sf_findBy q p     = (L.find p . L.filter q) `eqP` (S.findBy p . S.filter q)
 t_findBy p        = L.find p      `eqP` T.findBy p
 tl_findBy p       = L.find p      `eqP` TL.findBy p
-t_partition p     = L.partition p `eqP` (unpack2 . T.partition p)
-tl_partition p    = L.partition p `eqP` (unpack2 . TL.partition p)
+t_partition p     = L.partition p `eqP` (unpack2 . T.partitionBy p)
+tl_partition p    = L.partition p `eqP` (unpack2 . TL.partitionBy p)
 
 sf_index p s      = forAll (choose (-l,l*2)) ((L.filter p s L.!!) `eq` S.index (S.filter p $ packS s))
     where l = L.length s