Commits

Bryan O'Sullivan committed e547d46

Implement partition.

  • Participants
  • Parent commits a29e369

Comments (0)

Files changed (2)

File Data/Text.hs

     , elem
     , filter
     , find
-    -- , partition
+    , partition
 
     -- , findSubstring
     
 find p t = S.find p (stream t)
 {-# INLINE find #-}
 
+-- | /O(n)/ The 'partition' 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 #-}
+
 -- | Break a string on a substring, returning a pair of the part of the
 -- string prior to the match, and the rest of the string.
 --
         | null s             = (src,empty)      -- not found
         | pat `isPrefixOf` s = (take n src,s)
         | otherwise          = search (n+1) (tail s)
+{-# INLINE breakSubstring #-}
 
 -- | /O(n)/ 'filter', applied to a predicate and a 'Text',
 -- returns a 'Text' containing those characters that satisfy the

File tests/Properties.hs

 prop_elem c          = L.elem c      `eqP` T.elem c
 prop_filter p        = L.filter p    `eqP` (unpack . T.filter p)
 prop_find p          = L.find p      `eqP` T.find p
+prop_partition p     = L.partition p `eqP` (unpack2 . T.partition p)
 
 prop_index x s       = x < L.length s && x >= 0 ==>
                        (L.!!) s x == T.index (pack s) x
   ("prop_elem", mytest prop_elem),
   ("prop_filter", mytest prop_filter),
   ("prop_find", mytest prop_find),
+  ("prop_partition", mytest prop_partition),
 
   ("prop_index", mytest prop_index),
   ("prop_findIndex", mytest prop_findIndex),