Commits

Bryan O'Sullivan committed 8980730

Implement and test lazy elem, filter, find, and partition

Comments (0)

Files changed (2)

Data/Text/Lazy.hs

     , isInfixOf
 
     -- * Searching
-    -- , elem
-    -- , filter
-    -- , find
-    -- , partition
+    , elem
+    , filter
+    , find
+    , partition
 
     -- , findSubstring
     
 {-# INLINE isInfixOf #-}
 -- TODO: a better implementation
 
+-- | /O(n)/ 'elem' is the 'Text' membership predicate.
+elem :: Char -> Text -> Bool
+elem c t = S.elem c (stream t)
+{-# INLINE elem #-}
+
+-- | /O(n)/ 'filter', applied to a predicate and a 'Text',
+-- returns a 'Text' containing those characters that satisfy the
+-- predicate.
+filter :: (Char -> Bool) -> Text -> Text
+filter p t = unstream (S.filter p (stream t))
+{-# INLINE filter #-}
+
+-- | /O(n)/ The 'find' function takes a predicate and a 'Text',
+-- and returns the first element in matching the predicate, or 'Nothing'
+-- if there is no such element.
+find :: (Char -> Bool) -> Text -> Maybe Char
+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 #-}
+
 revChunks :: [T.Text] -> Text
 revChunks = L.foldl' (flip chunk) Empty
 

tests/Properties.hs

 prop_TL_isInfixOf s    = L.isInfixOf s `eqP` TL.isInfixOf (packT s)
 
 prop_T_elem c          = L.elem c      `eqP` T.elem c
+prop_TL_elem c         = L.elem c      `eqP` TL.elem c
 prop_T_filter p        = L.filter p    `eqP` (unpackT . T.filter p)
+prop_TL_filter p       = L.filter p    `eqP` (unpackT . TL.filter p)
 prop_T_find p          = L.find p      `eqP` T.find p
+prop_TL_find p         = L.find p      `eqP` TL.find p
 prop_T_partition p     = L.partition p `eqP` (unpack2 . T.partition p)
+prop_TL_partition p    = L.partition p `eqP` (unpack2 . TL.partition p)
 
 prop_T_index x s       = x < L.length s && x >= 0 ==>
                        (L.!!) s x == T.index (packT s) x
   ("prop_TL_isInfixOf", mytest prop_TL_isInfixOf),
 
   ("prop_T_elem", mytest prop_T_elem),
+  ("prop_TL_elem", mytest prop_TL_elem),
   ("prop_T_filter", mytest prop_T_filter),
+  ("prop_TL_filter", mytest prop_TL_filter),
   ("prop_T_find", mytest prop_T_find),
+  ("prop_TL_find", mytest prop_TL_find),
   ("prop_T_partition", mytest prop_T_partition),
+  ("prop_TL_partition", mytest prop_TL_partition),
 
   ("prop_T_index", mytest prop_T_index),
   ("prop_T_findIndex", mytest prop_T_findIndex),
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.