on master help sign in
Implement and test lazy elem, filter, find, and partition

--HG-- extra : convert_revision : fb33f9a9b9b583711e3fad7d6cf36c78e4e079ee

commit27ac2559407c8410c1f70bb11ef6107cbc6c06c8 authorBryan O'Sullivan <bos@serpentine.com> dateSun 8 Mar 2009, 03:29 (18 years ago) parent33fb4b3
2 files changed+40−4
Data/Text/Lazy.hs+32−4 tests/Properties.hs+8
Data/Text/Lazy.hs+32−4
@@ -125,10 +125,10 @@ module Data.Text.Lazy
125125 , isInfixOf
126126
127127 -- * Searching
128− -- , elem
129− -- , filter
130− -- , find
131− -- , partition
128+ , elem
129+ , filter
130+ , find
131+ , partition
132132
133133 -- , findSubstring
134134
@@ -781,6 +781,34 @@ isInfixOf needle haystack = L.any (isPrefixOf needle) (tails haystack)
781781{-# INLINE isInfixOf #-}
782782-- TODO: a better implementation
783783
784+-- | /O(n)/ 'elem' is the 'Text' membership predicate.
785+elem :: Char -> Text -> Bool
786+elem c t = S.elem c (stream t)
787+{-# INLINE elem #-}
788+
789+-- | /O(n)/ 'filter', applied to a predicate and a 'Text',
790+-- returns a 'Text' containing those characters that satisfy the
791+-- predicate.
792+filter :: (Char -> Bool) -> Text -> Text
793+filter p t = unstream (S.filter p (stream t))
794+{-# INLINE filter #-}
795+
796+-- | /O(n)/ The 'find' function takes a predicate and a 'Text',
797+-- and returns the first element in matching the predicate, or 'Nothing'
798+-- if there is no such element.
799+find :: (Char -> Bool) -> Text -> Maybe Char
800+find p t = S.find p (stream t)
801+{-# INLINE find #-}
802+
803+-- | /O(n)/ The 'partition' function takes a predicate and a 'Text',
804+-- and returns the pair of 'Text's with elements which do and do not
805+-- satisfy the predicate, respectively; i.e.
806+--
807+-- > partition p t == (filter p t, filter (not . p) t)
808+partition :: (Char -> Bool) -> Text -> (Text, Text)
809+partition p t = (filter p t, filter (not . p) t)
810+{-# INLINE partition #-}
811+
784812revChunks :: [T.Text] -> Text
785813revChunks = L.foldl' (flip chunk) Empty
786814
tests/Properties.hs+8
@@ -309,9 +309,13 @@ prop_T_isInfixOf s = L.isInfixOf s `eqP` T.isInfixOf (packT s)
309309prop_TL_isInfixOf s = L.isInfixOf s `eqP` TL.isInfixOf (packT s)
310310
311311prop_T_elem c = L.elem c `eqP` T.elem c
312+prop_TL_elem c = L.elem c `eqP` TL.elem c
312313prop_T_filter p = L.filter p `eqP` (unpackT . T.filter p)
314+prop_TL_filter p = L.filter p `eqP` (unpackT . TL.filter p)
313315prop_T_find p = L.find p `eqP` T.find p
316+prop_TL_find p = L.find p `eqP` TL.find p
314317prop_T_partition p = L.partition p `eqP` (unpack2 . T.partition p)
318+prop_TL_partition p = L.partition p `eqP` (unpack2 . TL.partition p)
315319
316320prop_T_index x s = x < L.length s && x >= 0 ==>
317321 (L.!!) s x == T.index (packT s) x
@@ -508,9 +512,13 @@ tests = [
508512 ("prop_TL_isInfixOf", mytest prop_TL_isInfixOf),
509513
510514 ("prop_T_elem", mytest prop_T_elem),
515+ ("prop_TL_elem", mytest prop_TL_elem),
511516 ("prop_T_filter", mytest prop_T_filter),
517+ ("prop_TL_filter", mytest prop_TL_filter),
512518 ("prop_T_find", mytest prop_T_find),
519+ ("prop_TL_find", mytest prop_TL_find),
513520 ("prop_T_partition", mytest prop_T_partition),
521+ ("prop_TL_partition", mytest prop_TL_partition),
514522
515523 ("prop_T_index", mytest prop_T_index),
516524 ("prop_T_findIndex", mytest prop_T_findIndex),